连接三个表的mysql case语句

连接三个表的mysql case语句,mysql,Mysql,有三张桌子 table a table b table c 我的问题是: select a.title,a.id,b.title from table a case when a.type=1 then inner join table a.id=tableb.id end case when a.type=2 then inner join table a.id=table c.id 但是这个查询不起作用。有人能帮你找到获取或执行这种类型查询的正确方法吗

有三张桌子

table a
table b 
table c
我的问题是:

select a.title,a.id,b.title 
  from table a 
  case when a.type=1 then 
       inner join table a.id=tableb.id end 
  case when a.type=2 then inner join table a.id=table c.id

但是这个查询不起作用。有人能帮你找到获取或执行这种类型查询的正确方法吗。要实现这一点,您可以使用UNION ALL。例如:

select a.title,a.id,b.title
from table a inner join table b on a.id=b.id
where a.type=1
UNION ALL
select a.title,a.id,c.title
from table a inner join table c on a.id=c.id
where a.type=2

您不能执行类似“如果这是1,则连接其他表,如果它是2”,您必须同时连接这两个表并相应地选择:

SELECT
  a.title, 
  a.id, 
  IF (tableb.title IS NOT NULL, tableb.title, tablec.title),
  CASE a.type
    WHEN 1 THEN tableb.id
    WHEN 2 THEN tablec.id
  END
FROM table a
LEFT JOIN tableb ON tablea.id = tableb.id
LEFT JOIN tablec ON tablea.id = tablec.id

你想干什么?CASE WHEN必须在SELECT语句中使用。您尝试这样做的事实表明您的模式违反了。修复它。如果表a的字段类型=1,则与表b进行联接;如果表1的字段类型=2,则与表CY进行联接。您必须用人类语言解释您的意图,因为您的SQL有许多语法和语义问题是可以理解的。在您的结果集中,没有用于
表c
的字段。它是否仅用于筛选数据?如何在此内部使用限制您可以在每个查询结束时使用限制,以防您需要对每个类别单独使用限制,或者将其包装在查询中并限制外部查询