0

I have 4 tables. (Simplified the 4 tables for the question)

Table 1: Supplier - Id, Type, TypeId, ExpiryDate

Table 2: SupplierType1 - Id, UserId, SupplierName

Table 3: SupplierType2 - Id, UserId, SupplierName

Table 4: User - Id, Name

All Id's are simply auto-increments in their tables.

In Supplier: 'Type' refers to either SupplierType1 or SupplierType2

'TypeId' is the 'Id' from either SupplierType1 or SupplierType2.

In both SupplierType1 and SupplierType2: 'UserId' is the Id from User

I have the 'UserId' (shown as X below). I want to return a list of which has these columns: Supplier.Id, SupplierType1.RetailName, ExpiryDate

Where (SupplierType1.UserId == 'X' AND Supplier.TypeId == SupplierType1 AND Supplier.ExpiryDate < CurrentDate)

I've only recently starting using mysql and my current solutions seem way too cumbersome. Keen to learn a better way to do this. I would appreciate the help. Thanks in advance!

1 Answer 1

2

query

$qry = "
SELECT * 
FROM Supplier s
LEFT JOIN SupplierType1 s1 ON s1.id=s.TypeId
LEFT JOIN User u ON u.Id=s1.UserId
WHERE s1.UserId='X' AND s.TypeId='SupplierType1' AND s.ExpiryDate<CURDATE()
";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Devs, perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.