Mysql 选择一个表并连接两个不同的表
我有3个表,书表是主表,女性和男性是次要的,我需要先通过条件连接第二个和第三个 范例书:Mysql 选择一个表并连接两个不同的表,mysql,sql,join,Mysql,Sql,Join,我有3个表,书表是主表,女性和男性是次要的,我需要先通过条件连接第二个和第三个 范例书: +------+---------+----------+ | b_id | b_title | b_author | +------+---------+----------+ | 1 | First | 3 | | 2 | Second | 1 | | 3 | Third | -4 | | 4 | test |
+------+---------+----------+
| b_id | b_title | b_author |
+------+---------+----------+
| 1 | First | 3 |
| 2 | Second | 1 |
| 3 | Third | -4 |
| 4 | test | -3 |
+------+---------+----------+
男:
女性:
+------+--------+
| f_id | f_name |
+------+--------+
| 1 | Sarah |
| 2 | Shanon |
| 3 | Nina |
| 4 | Shina |
| 5 | Mary |
+------+--------+
现在我需要从书中选择,当b_作者为正时,从男性表中选择,b_作者从女性表中选择
SELECT b_id,b_title, IF(b_author > 0,m_name,f_name) AS 'b_author' FROM book -- how make join here.
更重要的是,这是一种糟糕的数据库格式化方法。为什么不让“author”表中的列指定男性或女性?然后,使用一个外键(在本例中为author_id),您可以轻松地将表连接在一起。@ahjohnston25我不能使用其他表,因为该表是在我之前在公司设计的。
SELECT b_id,b_title, IF(b_author > 0,m_name,f_name) AS 'b_author' FROM book -- how make join here.
select *
from book b
left join male m on m.m_id=b.b_author
left join female f on f.f_id=b.b_author*-1