Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

php - MYSQL AND query to satisfy on same column

Project Aim :

We are developing bus timing Api where user will search for buses.

Following are my table structure

I have following tables

buses

id | bus_name

Description of table: Store all buses Names

routes

id | route_name

Description of table: Store All city names

stops

id | stop_name

Description of table: All stop names

stop_orders

id | route_id | stop_id | stop_order

Description of table: here i will assign stops for city and stop_order column help to identify which stop next to each other

bus_timing

id | stop_order_id | bus_id | bus_timing | trip | trip_direction

Description of table: Here i will assign buses for route stops along with time and trip and direction

Output Expecting:

  1. When user search between source to destination with time then Api must return all buses list with time

  2. if direct buses not there then interconnected buses should show

For example if user search between stop_8 to stop_18 with 01:00:00 to 12:00:00 then all buses list with time should show.if direct buses not there to travel between two stops then interconnected link buses list should show

Output what i got is

PHP compare associative array based on condition

Present return result issue is

  1. It will return all buses even though if bus is only travel to stop_8 but not stop_18.But my result must return only those buses which will travel between two stops i mean it must fall between both stops .

  2. Even i have no idea how to find interconnected buses list

  3. When time range is long then there is chance of same bus will travel(trip and direction) multiple times

Updates Still looking for answer .Right now given answer has some points so offered bounty

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because stop_id cannot be two different values in the same row.

Aggregation is one way to do what you want:

SELECT b.bus_name
FROM buses b JOIN
     route_connect rc
     ON rc.busid = b.id JOIN
     stops s
     ON s.id = rc.stop_id
GROUP BY b.bus_name
HAVING SUM( s.stop_name = 'Sydney' ) > 0 AND
       SUM( s.stop_name = 'Melbourne' ) > 0;

This returns buses that have stops with the name of both cities.

Given that buses can have lots of stops, it might be more efficient to do:

SELECT b.bus_name
FROM buses b JOIN
     route_connect rc
     ON rc.busid = b.id JOIN
     stops s
     ON s.id = rc.stop_id
WHERE s.stop_name in ('Sydney', 'Melbourne')
GROUP BY b.bus_name
HAVING COUNT(DISTINCT s.stop_name) = 2;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...