Php mySQL存在或不存在的位置选择
好了,伙计们,我对mySQL有个大问题。我需要做选择,在特定时间内获得餐厅的免费餐桌。这是我的桌子: +-------------+ | id | chairs| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+ 表: +-------------+ | id | chairs| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+ +-------------+ |id |椅子| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+ 和保留: +-------------+ | id | chairs| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+ +---------------------------------+ | id | table_id | start_datetime | +----------------|---------------------+ | 1 | 3 | 2013-11-27 16:15:00 | | 2 | 5 | 2013-11-27 19:00:00 | +----------------|---------------------+ +---------------------------------+ |id |表| id |开始|日期时间| +----------------|---------------------+ | 1 | 3 | 2013-11-27 16:15:00 | | 2 | 5 | 2013-11-27 19:00:00 | +----------------|---------------------+ 我试试这样的 +-------------+ | id | chairs| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+Php mySQL存在或不存在的位置选择,php,mysql,sql,Php,Mysql,Sql,好了,伙计们,我对mySQL有个大问题。我需要做选择,在特定时间内获得餐厅的免费餐桌。这是我的桌子: +-------------+ | id | chairs| +-------------+ | 1 | 3 | | 2 | 5 | | 4 | 10 | | 7 | 12 | | 10 | 6 | +-------------+ 表: +-------------+ | id | chairs| +-------------+ |
SELECT *
FROM wp_reserveit_tables,wp_reserveit_reservations
WHERE (wp_reserveit_tables.chairs = wp_reserveit_reservations.table_id
AND wp_reserveit_reservations.start_datetime - INTERVAL 1 HOUR >= '2013-11-29 16:15:00'
AND wp_reserveit_reservations.start_datetime + INTERVAL 1 HOUR <= '2013-11-29 16:15:00')
OR wp_reserveit_tables.chairs <> wp_reserveit_reservations.table_id
选择*
从wp_reserveit_表中,wp_reserveit_reservations
其中(wp_reserveit_tables.chairs=wp_reserveit_reservations.table_id
和wp_reserveit_reservations.start_datetime-INTERVAL 1小时>='2013-11-29 16:15:00'
和wp_reserveit_reservations.start_datetime+INTERVAL 1 HOURReservation表只有在有任何预订的情况下才会有条目。因此,我想我们可以简单地使用而不在子句中执行
+-------------+
| id | chairs|
+-------------+
| 1 | 3 |
| 2 | 5 |
| 4 | 10 |
| 7 | 12 |
| 10 | 6 |
+-------------+
SELECT * FROM wp_reserveit_tables WHERE chairs_id NOT IN
(SELECT table_id FROM wp_reserveit_reservations GROUP BY table_id)
在您的查询中,您已将它们与chairs列和table_id列合并。因此,我不确定哪一个匹配。因此,请根据您的要求更改Where子句中的列。您的查询中有列名start_datetime
,并且您的表结构将其指定为starttime
+-------------+
| id | chairs|
+-------------+
| 1 | 3 |
| 2 | 5 |
| 4 | 10 |
| 7 | 12 |
| 10 | 6 |
+-------------+
除非您进一步指定,否则如果不是打字错误,这看起来确实是一个有效的错误。您收到的错误是什么?您正在使用两个表数据尝试什么,它显示的错误是什么。?查询似乎功能正常,您可能希望使用非deprescated连接语法,尽管您的开始日期时间的列名不一致在你的查询和你的表格结构之间为什么你要把table.chairs加入table_id而不是table.id?伙计们,我要特别感谢你们,Mari。我一直在努力让它工作两周,你们在1小时或更短的时间内回答了这个问题。再次非常感谢你们。
+-------------+
| id | chairs|
+-------------+
| 1 | 3 |
| 2 | 5 |
| 4 | 10 |
| 7 | 12 |
| 10 | 6 |
+-------------+