MySQL SubQuery条件

时间:2013-05-15 09:54:31

标签: mysql subquery conditional

我正在尝试在MySQL中构建一个相当复杂的视图,并希望做一个有条件的,但它总是失败。

我的观点(简化)是

Select entry AS Entry, ,(select count(`poitems`.`entry`) AS `count(poitems.entry)` from `poitems` where (`poitems`.`PurchaseOrder` = `purchaseorder`.`entry`)) AS `TotalEntries`, from purchase orders

这没关系,但我要做的是添加类似

的内容
if ((select count(`poitems`.`entry`) = 0),'query.png',NULL) AS Queryflag

或只是测试TotalEntries的值。

帮助表示感谢!谢谢!

1 个答案:

答案 0 :(得分:0)

我对purchaseorderpoitems表格中的列名称并不是100%肯定,但以下内容应该让您朝着正确的方向前进:

select t.Entry,
case when t.TotalEntries > 0 then 'query.png' else null end as Queryflag
from
(
select po.entry as Entry,
count(*) as TotalEntries
from purchaseorder po
left outer join poitems poi on poi.purchaseorder = po.entry
group by po.entry
) t;