1)表1)
这是一个表运算符,有三列id是primary_key。
mysql> select id, name, status from operators limit 10;
+----+-----------------------+---------------+
| id | name | status |
+----+-----------------------+---------------+
| 1 | abcde Travels | Active |
| 2 | bcdef Tourists | Active |
| 3 | cdefg Travels | Active |
| 4 | defgh Travels | Active |
| 5 | efghi Travels | Active |
| 6 | fghij Travels | Active |
| 7 | ghijk Travels | Paid Operator |
| 8 | hijkl Travels | Active |
| 9 | ijklm Travels | Active |
| 10 | jklmn Travels | Paid Operator |
+----+-----------------------+---------------+
2)表2
这是operator_api_config表,我们将operator_id作为foreign_key并将lcc作为一列。
mysql> select id, operator_id,lcc from operator_api_configs limit 10;
+----+-------------+------+
| id | operator_id | lcc |
+----+-------------+------+
| 1 | 1 | 0.00 |
| 2 | 2 | 2.00 |
| 3 | 3 | 0.00 |
| 4 | 4 | 0.00 |
| 5 | 5 | 0.00 |
| 6 | 6 | 0.00 |
| 7 | 7 | 0.00 |
| 8 | 8 | 0.00 |
| 9 | 9 | 0.00 |
| 10 | 10 | 3.00 |
+----+-------------+------+
3)表3
这是主表计划,其中有多列,operator_id是foreign_key。
mysql> select id, origin_id,destination_id,departure_time,available_seats, operator_id,ADDTIME(TIMESTAMP(travel_date,(STR_TO_DATE(departure_time,'%h:%i%p'))), duration) as arrival_date_time from schedules limit 10;
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| id | origin_id | destination_id | departure_time | available_seats | operator_id | arrival_date_time |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| 797263 | 134 | 251 | 10:45 PM | 0 | 5 | 2014-08-26 06:00:00 |
| 794764 | 134 | 251 | 10:15 PM | 19 | 4 | 2014-08-26 05:00:00 |
| 794892 | 134 | 251 | 11:30 PM | 51 | 5 | 2014-08-26 06:00:00 |
| 794936 | 134 | 251 | 03:00 PM | 51 | 10 | 2014-08-25 22:00:00 |
| 776582 | 134 | 251 | 07:30 AM | 0 | 7 | 2014-08-25 16:30:00 |
| 776612 | 134 | 251 | 10:30 PM | 25 | 7 | 2014-08-26 06:00:00 |
| 776591 | 134 | 251 | 10:15 AM | 25 | 7 | 2014-08-25 17:45:00 |
| 776622 | 134 | 251 | 11:15 PM | 29 | 8 | 2014-08-26 07:15:00 |
| 776601 | 134 | 251 | 03:30 PM | 0 | 10 | 2014-08-25 23:15:00 |
| 776661 | 134 | 251 | 09:45 PM | 33 | 10 | 2014-08-26 05:45:00 |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
预期产出:
1)付费运营商(运营商表中的状态)计划应该在列表中首先出现(我需要在UI上显示的列表),其中包含available_seats>在时间表中为0。
2)在付费运营商中,应首先调度哪个运营商拥有更多lcc(在operator_api_config表中)。
3)完整列表应按出发时间的升序排列。
4)所有available_seats<在计划表中的0应该下降(所有付费运营商和活动运营商也是如此)。
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| id | origin_id | destination_id | departure_time | available_seats | operator_id | arrival_date_time |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| 794936 | 134 | 251 | 03:00 PM | 51 | 10 | 2014-08-25 22:00:00 |
| 776661 | 134 | 251 | 09:45 PM | 33 | 10 | 2014-08-26 05:45:00 |
| 776591 | 134 | 251 | 10:15 AM | 25 | 7 | 2014-08-25 17:45:00 |
| 776612 | 134 | 251 | 10:30 PM | 25 | 7 | 2014-08-26 06:00:00 |
| 794764 | 134 | 251 | 10:15 PM | 19 | 4 | 2014-08-26 05:00:00 |
| 776622 | 134 | 251 | 11:15 PM | 29 | 8 | 2014-08-26 07:15:00 |
| 794892 | 134 | 251 | 11:30 PM | 51 | 5 | 2014-08-26 06:00:00 |
| 776582 | 134 | 251 | 07:30 AM | 0 | 7 | 2014-08-25 16:30:00 |
| 776601 | 134 | 251 | 03:30 PM | 0 | 10| 2014-08-25 23:15:00 |
| 797263 | 134 | 251 | 10:45 PM | 0 | 5 | 2014-08-26 06:00:00 |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
我尝试了这个查询:
select sch.id, sch.origin_id,sch.destination_id, sch.departure_time,sch.available_seats, sch.operator_id, ADDTIME(TIMESTAMP(travel_date,(STR_TO_DATE(departure_time,'%h:%i%p'))), duration) as arrival_date_time from schedules as sch
INNER JOIN operators as ope on sch.operator_id = ope.id
INNER JOIN operator_api_configs as opconf on ope.id = opconf.operator_id
WHERE ((ADDTIME(TIMESTAMP(sch.travel_date,(STR_TO_DATE(sch.departure_time,'%h:%i%p'))), sch.duration) >= NOW())) and sch.origin_id = '134' and sch.destination_id = '251'
ORDER BY ope.status desc, opconf.lcc desc, (sch.available_seats <= 0 && (STR_TO_DATE(sch.departure_time,'%h:%i%p') >= TIME(NOW()))), STR_TO_DATE(sch.departure_time,'%h:%i%p');
输出:
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| id | origin_id | destination_id | departure_time | available_seats | operator_id | arrival_date_time |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
| 794936 | 134 | 251 | 03:00 PM | 51 | 10 | 2014-08-25 22:00:00 |
| 776601 | 134 | 251 | 03:30 PM | 0 | 10 | 2014-08-25 23:15:00 |
| 776661 | 134 | 251 | 09:45 PM | 33 | 10 | 2014-08-26 05:45:00 |
| 776582 | 134 | 251 | 07:30 AM | 0 | 7 | 2014-08-25 16:30:00 |
| 776591 | 134 | 251 | 10:15 AM | 25 | 7 | 2014-08-25 17:45:00 |
| 776612 | 134 | 251 | 10:30 PM | 25 | 7 | 2014-08-26 06:00:00 |
| 794764 | 134 | 251 | 10:15 PM | 19 | 4 | 2014-08-26 05:00:00 |
| 776622 | 134 | 251 | 11:15 PM | 29 | 8 | 2014-08-26 07:15:00 |
| 794892 | 134 | 251 | 11:30 PM | 51 | 5 | 2014-08-26 06:00:00 |
| 797263 | 134 | 251 | 10:45 PM | 0 | 5 | 2014-08-26 06:00:00 |
+--------+-----------+----------------+----------------+-----------------+-------------+---------------------+
这里我想要所有的available_seats&lt; 0应该按照departure_time coulmn的顺序排列......