加入IF条件?

时间:2012-06-05 03:49:31

标签: mysql join mysqli

我有我的主表,我有2个表将被加入。如果员工是男性,我想加入表A,如果员工是女性,我想加入表B.有可能这样做吗?

2 个答案:

答案 0 :(得分:2)

怎么样

  SELECT ....
  FROM main JOIN A ON ...
  WHERE male
UNION ALL
  SELECT ....
  FROM main JOIN B ON ...
  WHERE female

答案 1 :(得分:1)

您仍然可以加入两个表,但使用CASE语句来决定要从哪个表中读取。

SELECT CASE WHEN Table.Field = 'Male' THEN A.Field ELSE B.Field END AS Something
  FROM Table
  LEFT JOIN A on condition
  LEFT JOIN B on condition