繁体   English   中英

MySQL从一个或另一个表中选择

[英]mysql select from one or another table

谢谢您的帮助,我发布了问题的简化版本,但我真的不明白如何在大连接中应用左连接:

SELECT d.type, 
       d.item , 
       if(d.type='I', a.name, b.name) as name, 
       if(d.type='I', c.price,0) as price, 
       if(d.type='I',if(d.taxes='yes', 
       (c.priceWithTax*d.weight), (c.price*d.weight)),0) as totalprice 
  FROM d 
inner join a on d.item=a.id 
inner join c on d.item=c.item
     where c.sede =1

问题是,当d.type ='I'我需要表a中的商品,但是如果d.type ='S'我需要表B中的商品,则价格在表c上。

非常感谢你。

如果您需要类似的内容,则很明显表明您的数据库结构错误。
它应该是一张表,并且您的查询变得简单明了。

select
  a.col1,
  b.col1,
  coalesce(c.col1,0)
from a inner join b on a.col0=b.col1
  left outer join c on b.col2='apple' and c.col1=b.col0

要学习的东西:


我有一个rawFood表,cookedFood表和一个Menu表,并且该菜单表同时包含rawFood和CookedFood中的内容,这就是为什么我需要以这种方式加入

您需要有一个food表,并在其中提供一栏,以记录生食和熟食之间的差异。

您可以使用左联接

select
    a.col1,
    b.col1,
    ifnull(c.col1,0)
from a 
    inner join b on a.col0=b.col1
    left join c on (c.col1 = b.col0 and b.col2="apple")
where
    b.col2 != "apple" or (b.col2 = "apple" and c.col1 is not null)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM