mysql连接三个表的case语句

标签 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

但是这个查询不起作用。有人可以帮助提供正确的方法来获取或执行此类查询

最佳答案

不能在 from 子句中使用 case。为了实现这一点,您可以使用 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

关于mysql连接三个表的case语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23054449/

相关文章:

mysql - 如何从本地主机将 SQL 数据库导入 R?

python - 元素移动日常采集数据库设计系统问题

php - MySQL高分查询

php - 哪个更快/更聪明,为什么是 : COUNT(*) or storing the numbers each the do something

php - 如何检查MySQL中是否存在一行? (即检查 MySQL 中是否存在用户名或电子邮件)

php - 如何将一张表的 id 值获取到另一张表

mysql - NULL 值的总和、平均值、最大值、最小值、计数

mysql - 将基于字符串的结构转换为基于自动增量的树结构

php - 在 php 中将 MySQL 查询写入 CSV

mysql - SQL:按foreign_key数字列表排序