加入查询

时间:2012-04-30 18:56:29

标签: mysql join

本周我正在为考试做准备,我看到了过去这样的论文问题:

讲师

lec_id
lec_name
phone
dept

请求

lec_id
book_id 
qty 
date

图书

book_id
title
author
cost
supplier

我需要编写一个MySQL命令,找到每位讲师的姓名和电话号码,请求ID为#123的书。

我尝试创建查询,如下所示:

SELECT lec_name, phone FROm lecturer 
  LEFT JOIN requests ON requests.lec_id = lecturer.lec_id 
  LEFT JOIN requests ON requests.book_id = book.book_id
WHERE book.book_id = 123

但是我一直收到错误Not unique table / alias:'requests'

2 个答案:

答案 0 :(得分:0)

最好从本书开始,以便WHERE子句更清晰,并且可能更直观地适应搜索其他特定书籍和/或所有书籍:

select book.book_id, book.title, lecturer.lec_name, lecturer.phone 
from book
left join requests on book.book_id = requests.book_id
left join Lecturer on requests.lec_id = Lecturer.lec_id 
where book.book_id = 123

答案 1 :(得分:0)

SELECT lec_name, phone FROm lecturer 
  LEFT JOIN requests ON requests.lec_id = lecturer.lec_id 
  AND requests.book_id = book.book_id
WHERE book.book_id = 123