复杂的选择比较

时间:2013-10-14 20:35:17

标签: mysql sql select

嘿伙计们我有两张桌子T1和T2,T1有一个叫做时间的柱子,T2有两个柱子的日期和时间。遵循方案

T1
-------------------
Time
-------------------
10:00
11:00
12:00

T2
-------------------
Date       | Time
-------------------
2013-10-01 | 10:00
2013-10-01 | 11:00
2013-10-01 | 12:00
2013-10-02 | 10:00
2013-10-02 | 11:00

我需要写一个返回2013-10-01的选项,因为此日期的时间与所有 T1字段匹配。这可能吗?

1 个答案:

答案 0 :(得分:1)

只要您在T1

中有3个值,这将有效
SELECT T2.Date
FROM T2
inner join T1 on T1.Time = T2.Time
GROUP BY T2.Date
HAVING COUNT(DISTINCT T1.Time) = 3

或更多“通用”但性能较差:

SELECT T2.Date
FROM T2
INNER JOIN T1 on T1.Time = T2.Time
GROUP BY T2.Date
HAVING COUNT(DISTINCT T1.Time) = (select count(distinct Time) from T1);

SqlFiddle