I have three tables like this:
我有三个这样的表:
Specialisation
专业化
sid | s_name
--------------
1 | test 1
2 | test 2
Person
人
pid | name | sid
------------------
1 | ABC | 1
2 | XYZ | 2
Timing
定时
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
**I want to get result something like this*
**我想得到像这样的结果*
pid | name | s_name | time_from | time_to
--------------------------------------------
1 | ABC | test 1 | 08:00:00 | 10:00:00
Description:
All three tables are connected.
I want all records where
specialisation id = '1'
person name Like 'ABC'
timing is in between '08:00:00' and '10:00:00'.
I tried several combinations of mysql joins but not able to fetch the data correctly.
描述:所有三个表都已连接。我想要所有记录,其中specialized id ='1'人名像'ABC'时间在'08:00:00'和'10:00:00'之间。我尝试了几种mysql连接组合,但无法正确获取数据。
6 个解决方案
#1
19
You can use INNER JOIN
for this,
你可以使用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'
- SQLFiddle Demo
- SQLFiddle演示
#2
1
You can use Simple Join like this
您可以像这样使用简单连接
Select P.pid , name , s_name ,time_from ,
time_to from Specialisation S
Join Person P on P.sid=S.sid
Join Timing T on T.pid= P.pid
where T.time_from >= '08:00:00' AND T.time_to <='10:00:00' And P.name='ABC';
小提琴
#3
0
SELECT B.PID pid, B.NAME name, A.SNAME s_name, C.TIME_FROM time_from, C.TIME_TO time_to
FROM SPECIALISATION A, PERSON B, TIMING C
WHERE A.SID = 1 AND A.SID=B.SID AND B.PID=C.PID
AND C.TIME_FROM >=08:00:00 AND C.TIME_TO <=10.00.00
#4
0
Try :
试试:
SELECT t.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Specialisation AS s
left join Person AS p ON p.sid = s.sid
left join Timing AS t ON t.pid = p.pid
WHERE s.sid = 1
AND t.time_from >= 08:00:00' AND t.time_to <= 10:00:00
group by t.tid
#5
0
This is a straight forward JOIN
with appropriate WHERE
condition.
这是一个具有适当WHERE条件的直接JOIN。
SELECT t.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Timing AS t
JOIN Person AS p ON p.pid = t.pid
JOIN Specialisation AS s ON s.sid = p.sid
WHERE s.sid = 1
AND t.time_from >= 08:00:00 AND t.time_to <= 10:00:00
This is a simple condition where you have to use JOIN
s to fetch data this way. Its so simple that you will get a hang of it right after your first use. May be this question and answers will help you understand it.
这是一个简单的条件,您必须使用JOIN以这种方式获取数据。它非常简单,您可以在第一次使用后立即获得它。可能是这个问题和答案将帮助您理解它。
#6
0
SELECT p.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Timing AS t
JOIN Person AS p ON p.pid = t.pid
JOIN specialisation AS s ON s.sid = p.sid
WHERE s.sid = 1 and p.name='ABC'
AND t.time_from >= '08:00:00' AND t.time_to <= '10:00:00'
#1
19
You can use INNER JOIN
for this,
你可以使用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'
- SQLFiddle Demo
- SQLFiddle演示
#2
1
You can use Simple Join like this
您可以像这样使用简单连接
Select P.pid , name , s_name ,time_from ,
time_to from Specialisation S
Join Person P on P.sid=S.sid
Join Timing T on T.pid= P.pid
where T.time_from >= '08:00:00' AND T.time_to <='10:00:00' And P.name='ABC';
小提琴
#3
0
SELECT B.PID pid, B.NAME name, A.SNAME s_name, C.TIME_FROM time_from, C.TIME_TO time_to
FROM SPECIALISATION A, PERSON B, TIMING C
WHERE A.SID = 1 AND A.SID=B.SID AND B.PID=C.PID
AND C.TIME_FROM >=08:00:00 AND C.TIME_TO <=10.00.00
#4
0
Try :
试试:
SELECT t.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Specialisation AS s
left join Person AS p ON p.sid = s.sid
left join Timing AS t ON t.pid = p.pid
WHERE s.sid = 1
AND t.time_from >= 08:00:00' AND t.time_to <= 10:00:00
group by t.tid
#5
0
This is a straight forward JOIN
with appropriate WHERE
condition.
这是一个具有适当WHERE条件的直接JOIN。
SELECT t.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Timing AS t
JOIN Person AS p ON p.pid = t.pid
JOIN Specialisation AS s ON s.sid = p.sid
WHERE s.sid = 1
AND t.time_from >= 08:00:00 AND t.time_to <= 10:00:00
This is a simple condition where you have to use JOIN
s to fetch data this way. Its so simple that you will get a hang of it right after your first use. May be this question and answers will help you understand it.
这是一个简单的条件,您必须使用JOIN以这种方式获取数据。它非常简单,您可以在第一次使用后立即获得它。可能是这个问题和答案将帮助您理解它。
#6
0
SELECT p.pid, p.name, s.s_name, t.time_from, t.time_to
FROM Timing AS t
JOIN Person AS p ON p.pid = t.pid
JOIN specialisation AS s ON s.sid = p.sid
WHERE s.sid = 1 and p.name='ABC'
AND t.time_from >= '08:00:00' AND t.time_to <= '10:00:00'