在mysql连接查询中需要帮助

时间:2011-06-19 12:06:45

标签: php mysql

我正在一个网站上工作,我有三张桌子 1: - 图书作者 2: - 书籍类型 3: - 书籍关系

My first table have following fields 
-------------------
id  Author_name
1   abc
2   xyz
------------------
Second table

id  book_type
1   Politics
2   Religious

------------------
Third table

id   book_type_id   author_id
1       1             1
2       1             2

----------------------

在我的网页上,当访问者点击某个类别时,我会收到book_type。现在我想让作者姓名从作者姓名表中获得类别1。我想我已经解释清楚了。如果有人能解决这个问题......谢谢

2 个答案:

答案 0 :(得分:3)

select * from book_author where id in (select author_id from book_relationship where book_type_id = 'selected_category_id')

答案 1 :(得分:0)

这是另一个只使用连接的版本,因为你只有一个字符串类型:

SELECT A.* FROM `type` T 
LEFT JOIN `authtype` AT ON T.id=AT.authid 
LEFT JOIN `authors` A on AT.`authid`=A.`id` 
WHERE T.`type`="Religion"
相关问题