我的Select命令出现问题,首先,我需要在其他表上显示与idHour不同的所有小时数,这是代码:
SELECT DISTINCT Hours
FROM agenda_hours A
JOIN appoitment_hour B
ON (A.idHours <> B.Hour);
结果:
+----------+ | Hours | +----------+ | 07:00 | | 07:30 | | 08:00 | | 08:30 | | 09:00 | | 09:30 | | 10:00 | | 10:30 | | 11:00 | | 11:30 | | 12:00 | | 12:30 | | 13:00 | | 13:30 | | 14:00 | | 14:30 | | 15:00 | | 15:30 | | 16:00 | | 16:30 | | 17:00 | | 17:30 | | 18:00 | | 18:30 | | 19:00 | | 19:30 | | 20:00 | +----------+
答案 0 :(得分:2)
尝试这种方式:
SELECT DISTINCT Hours
FROM agenda_hours A
LEFT JOIN appoitment_hour B
ON (A.idHours = B.Hour)
WHERE B.Hour is null;
答案 1 :(得分:0)
你想要的是什么(是来自agenda_hours的小时数?)
SELECT DISTINCT Hours
FROM agenda_hours
WHERE idHours NOT IN (SELECT DISTINCT Hour FROM appoitment_hour)
您遇到问题导致JOIN尝试匹配表中的所有行,而另一行中的所有行都处于ON状态。因此,即使一个小时已经在另一个表中,该行也将与所有其他行匹配
答案 2 :(得分:0)
根据哪个表格包含Hours
列,您可以使用此查询:
SELECT /*DISTINCT*/ Hours
FROM agenda_hours A
WHERE NOT EXISTS(SELECT * FROM appoitment_hour B WHERE A.idHours = B.Hour);
或者这个:
SELECT /*DISTINCT*/ Hours
FROM appoitment_hour B
WHERE NOT EXISTS(SELECT * FROM agenda_hours A WHERE A.idHours = B.Hour);
答案 3 :(得分:0)
用于获取非匹配记录
SELECT b.Hour, a.idHours FROM appoitment_hour b
LEFT JOIN agenda_hours a ON b.Hour = a.idHours
GROUP BY b.Hour HAVING a.idHours IS NULL