我有三个这样的表:
特化
sid | s_name
--------------
1 | test 1
2 | test 2
人
pid | name | sid
------------------
1 | ABC | 1
2 | XYZ | 2
时间
tid | time_from | time_to | pid
----------------------------------
1 | 08:00:00 | 10:00:00 | 1
2 | 20:00:00 | 22:00:00 | 1
3 | 09:00:00 | 14:00:00 | 2
4 | 19:00:00 | 20:00:00 | 2
**我想得到这样的结果*
pid | name | s_name | time_from | time_to
--------------------------------------------
1 | ABC | test 1 | 08:00:00 | 10:00:00
描述:
所有三个表都已连接。
我想要所有记录
特化 id = '1'
人姓名喜欢 'ABC'
时间在'08:00:00'和'10:00:00'之间。
我尝试了几种 mysql 连接组合,但无法正确获取数据。
最佳答案
您可以为此使用 INNER JOIN
,
SELECT a.pid, a.name,
b.sname,
c.time_from,
c.time_to
FROM person a
INNER JOIN specialisation b
ON a.sid = b.sid
INNER JOIN Timing c
ON a.pid = c.pid
WHERE a.sid = 1 and
a.name='ABC' AND
c.time_from >= '08:00:00' AND c.time_to <= '10:00:00'
关于Mysql对三张多列表的join查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302898/