简体   繁体   English

PHP MYSQL 根据两个匹配的行列检索数据

[英]PHP MYSQL retrieve data based on two matching row column

I have a table where i need to check or compare two rows two column are equal then only i need to retrieve data我有一个表,我需要检查或比较两行两列相等然后我只需要检索数据

my table structure is我的表结构是

id | route_id | stop_id | bus_id | bus_time | trip | direction

if i execute following query如果我执行以下查询

select routes.route_name,stops.stop_name,buses.bus_name,bus_timings.bus_time,bus_timings.trip,bus_timings.bus_direction from `bus_timings` 
inner join `stop_orders` on `stop_orders`.`id` = `bus_timings`.`stop_order_id` 
inner join `routes` on `stop_orders`.`route_id` = `routes`.`id` 
inner join `stops` on `stop_orders`.`stop_id` = `stops`.`id` 
inner join `buses` on `buses`.`id` = `bus_timings`.`bus_id` 
where `stops`.`stop_name` in ("sydney","melborne")

output输出

1 | route_1  | Sydney | bus_1 | 07:05 :00 | 1 |1
2 | route_1 | Melbourne| bus_2 | 07:35:00 |1 |1

but in existing query even if bus is not traveling between Sydney also i will get Melbourne result .但在现有查询中,即使巴士不在悉尼之间行驶,我也会得到墨尔本的结果。 I need to get only buses name which falls or travel between Sydney to Melbourne我只需要获取在悉尼和墨尔本之间落下或旅行的巴士名称

simple way to do that , without much fuzz , and as u mentioned using PHP, so use only one where clause in the query statement and than filter the data using php, for example , after you retrieve the buses between Sydney and Melbourne, make a foreach loop and use if condition with selected times.这样做的简单方法,没有太多模糊,正如你提到的使用 PHP,所以在查询语句中只使用一个 where 子句,而不是使用 php 过滤数据,例如,在检索悉尼和墨尔本之间的公共汽车后,做一个foreach 循环并在选定时间使用 if 条件。

another way us that if you like using arrays match the times in each record using any array search function and get the keys out to another array.另一种方式是,如果您喜欢使用数组,则可以使用任何数组搜索函数匹配每个记录中的时间,并将键取出到另一个数组中。 the result array will contain the filtered data with buses between cities and between selected time.结果数组将包含城市之间和选定时间之间公交车的过滤数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM