我有3张桌子,书桌是主表,女性和男性是次要的,我需要先通过条件加入第二和第三。
示例书:
+------+---------+----------+
| b_id | b_title | b_author |
+------+---------+----------+
| 1 | First | 3 |
| 2 | Second | 1 |
| 3 | Third | -4 |
| 4 | test | -3 |
+------+---------+----------+
雄性:
+------+--------+
| m_id | m_name |
+------+--------+
| 1 | John |
| 2 | Jim |
| 3 | Jessy |
| 4 | Mike |
| 5 | Tom |
+------+--------+
雌性:
+------+--------+
| f_id | f_name |
+------+--------+
| 1 | Sarah |
| 2 | Shanon |
| 3 | Nina |
| 4 | Shina |
| 5 | Mary |
+------+--------+
现在我需要从书中选择,当b_author为肯定时,从男性表中选择,b_author从女性表中选择。
SELECT b_id,b_title, IF(b_author > 0,m_name,f_name) AS 'b_author' FROM book -- how make join here.
答案 0 :(得分:2)
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