I have two tables (Apple and Bid). I want to return all rows from A based on a given condition, and I want to get a single value from table B. It will help if I show what I’m trying to do:
Apple
ID
Name
Type
Bid
bid_id
Type
I want to return all records from A where the ID matches a given ID AND I want to get the bid_id associated with each record pulled from table A.
So, my query looks something like this:
"Select A.ID, A.Name, A.Type, B.bid_id
FROM Apple A
LEFT JOIN Bid B on B.Type = A.Type
Where A.ID = 35";
There is only one record in the Apple table with an id of 35. However, since there are three tables in the Bid table whose type matches that of Apple # 35’s type, three records are being returned.
To clarify, only one record should be being returned.
Any ideas?
For MySQL:
That will return one of the
bid_id‘s. If you have some rule as to whichbid_idto choose, you can implement it using aggregation functions – e.g., MIN, MAX, and more complex rules can be implemented as well.