MySql按书名和作者姓名

时间:2017-09-14 12:22:25

标签: mysql select

我有3张桌子, 1)书籍

+----+--------------+------+
| id | name         | code |
+----+--------------+------+
|20  | Lucian house | 2214 +
|21  | Lucian mom   | 2214 |
|22  | GoT          | 2214 |
+----+--------------+------+

2)producs_authors

+----+---------+-----------+
| id | book_id | author_id |
+----+---------+-----------+
|1   | 20      |    5      |
|2   | 21      |    5      |
|3   | 22      |    7      |
+----+---------+-----------+

3)作者

+----+--------------+
| id | name         |
+----+--------------+
| 5  | Marta        |
+----+--------------+
| 7  | Lucian       |
+----+--------------+

我想选择所有名字中包含Lucian的书籍,以及作为作者“Lucian”的所有书籍,我该怎么做? 我需要使用子查询吗? 到目前为止,我有这个(我使用yii框架),这就是标准的外观:

SELECT * 
FROM books t 
LEFT JOIN products_authors a ON a.id_book = t.id 
LEFT JOIN authors b ON b.id = a.author_id 
WHERE t.name LIKE %Lucian% 
OR b.name LIKE %Lucian%

2 个答案:

答案 0 :(得分:0)

试试这个 -

someReallyBigData

答案 1 :(得分:0)

请尝试这个mysql查询

select id,name from books where name like 'Lucian%' || id IN (select pa.book_id from producs_authors as pa,authors as a where pa.author_id=a.id && a.name= 'Lucian')