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
我的表结构是
id | route_id | stop_id | bus_id | bus_time | trip | direction
如果我执行以下查询
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")
输出
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
推荐答案
一种简单的方法,没有太多的绒毛,正如您提到的使用PHP, 因此,在查询语句中仅使用一个where子句,而不是使用php过滤数据, 例如 , 检索悉尼和墨尔本之间的巴士后,进行一次foreach循环,并在选定的时间使用if条件.
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.
另一种方式,如果您喜欢使用数组 使用任何数组搜索功能匹配每个记录中的时间,然后将键移到另一个数组. 结果数组将包含过滤后的数据,其中包括城市之间以及选定时间之间的公交车.
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.
这篇关于PHP MYSQL根据两个匹配的行列检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!