来自多个表的SQL查询

时间:2012-12-22 18:08:37

标签: sql

我是SQL的新手,我需要一些帮助,

我想查找已确认(已确认= 1)的姓氏为Steven

的预订日期

有些人可以使用此SQL查询

table_member

+-----------+-----------+
|Member_id  | last_name |
+-----------+-----------+
| 1         | David     |
| 2         | owen      |
| 3         | Tom       |
| 4         | Steven    |
| 5         | Steven    |
| 6         | Steven    |
| 7         | Steven    |
| 8         | Steven    |
| 9         | Steven    |
+-----------+-----------+

table_resevation

+-----------+-----------+-----------+-----------+
|resvationID|Member_id  | day       |   month   |
+-----------+-----------+-----------+-----------+
| 1         | 1         |   1       |     6     |
| 2         | 1         |   2       |     6     |
| 3         | 2         |   1       |     6     |
| 4         | 4         |   1       |     6     |
| 5         | 4         |   2       |     6     |
| 6         | 5         |   1       |     6     |
| 7         | 5         |   2       |     6     |
| 8         | 7         |   3       |     6     |
+-----------+-----------+-----------+-----------+

确认

+-----------+-----------+-----------+
|confirmID  |resvationID| confirmed |
+-----------+-----------+-----------+
| 1         | 1         |   1       |
| 2         | 2         |   1       |
| 3         | 3         |   0       |
| 4         | 4         |   0       |
| 5         | 5         |   1       |
| 6         | 6         |   1       |
| 7         | 7         |   0       |
| 8         | 8         |   1       |
+-----------+-----------+-----------+

3 个答案:

答案 0 :(得分:3)

这样的事情对你有用吗?

 SELECT
     m.Member_ID, m.Last_Name,
     r.Day, r.Month, 
     c.ConfirmID
 FROM 
     table_member m
 INNER JOIN 
     table_reservation r ON r.Member_ID = m.Member_ID
 INNER JOIN
     Confirm c ON c.resvationID = r.resvationID
 WHERE
     m.Last_Name = 'Steven' AND c.confirmed = 1

答案 1 :(得分:0)

Select c.*, r.*, m.*
from confirm c
left join table_reservation r on c.reservationID = r.reservationID
left join table_member m on m.member_id = r.Member_id
Where c.confirmed = 1
and m.name = "Steven"

我没有对它进行测试,但这应该指明你的方式。

(编辑:第二个“其中”是一个错字)

答案 2 :(得分:0)

在所有表格上应用内部联接

select day,month from table_reservation inner join table_member on table_member.member_id=table_reservation.member_id
inner join confirm on confirm.reservationid=table_reservation.reservationid where confirm.confirmid=1
and table_member.last_name like 'Steven'