2

这周我正在准备考试,我看到过去的试卷问题是这样的:

讲师

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'

4

2 回答 2

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
于 2012-05-04T06:15:30.083 回答
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
于 2012-05-01T05:56:17.477 回答