Mysql Select Statement with respect to another table field
I have two tables. One is newBus, and Second is newPa开发者_开发知识库ssengers. The table newBus contain the columns startcity and endCity. Now let say newBus.startCity contains:
newBus.id----newBus.startCity-----newBus.endCity
4 ACity to xCity
5 tCity to MCity
newPassenger table: 3 passengers wants to go to
newPassengers.s_city------newPassengers.e_city
tCity to GCity
OCity to FCity
tCity to MCity
I want to select the all passenger who want to go from tCity to MCity but with respect to newBus.id = 5.
SELECT newPassengers.id
FROM newBus
INNER JOIN newPassengers ON newPassengers.s_city = newBus.startCity
WHERE newPassengers.s_city = 'tCity'
AND newPassengers.e_city = 'MCity'
AND newBus.id = 5
This assumes that all passengers will get on a bus that has a start city the same as their start city, and that the ID of the passenger is newPassengers.id . You can add more fields to the select list to get the information you are after.
I want to select the all passenger who want to go from tCity to MCity but with respect to newBus.id = 5.
SELECT p.id
FROM newpassengers p
INNER JOIN newbus b ON (p.s_city = b.startcity AND p.e_city = b.endCity)
WHERE p.s_city = 'tCity' AND p.e_city = 'MCity' AND b.id = '5';
精彩评论