MySQL if语句条件连接 [英] MySQL if statement conditional join

查看:160
本文介绍了MySQL if语句条件连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我试图做点什么

SELECT * from accounttable, peopletable, companytable 
WHERE if accounttable.account_type = company 
JOIN companytable 
WHERE companytable.id = accounttable.company_id 
ELSE IF accounttable.account_type = = person 
JOIN peopletable 
  WHERE peopletable.id = accounttable.person_id

我很遗憾它有点sqlenglish但我真的不知道怎么写出来!

I'm sorry its a bit sqlenglish but I really dont know how to write it out!

推荐答案

如下所示:

SELECT * 
from accounttable
left join peopletable ON (accounttype = 'person' AND peopletable.id = accounttable.person_id)
left join companytable ON (accounttype = 'company' AND companytable.id = accounttable.company_id)

我会加入两个表,所以你会有输出中所有三个表的字段。来自 peopletable 的字段将为 NULL 如果 accounttype!='person' companytable 的那些将是 NULL 其中 accounttype!='company'

I'll join against both tables, so you'll have fields of all three tables in the output. The fields from peopletable will be NULL if accounttype != 'person', and those of companytable will be NULL where accounttype != 'company'.

您可以在 LEFT JOIN 语法的更多信息href =https://encrypted.google.com/search?sourceid=chrome&ie=UTF-8&q=left%20join>此处 ...

You can find more on the LEFT JOIN syntax in places like here...

这篇关于MySQL if语句条件连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆