SubQueries - 仅使用一个Where语句

时间:2010-10-29 11:50:36

标签: mysql subquery

我有以下查询,每次我更改它我需要更改stockclientid。我希望它只有一个where语句适用于所有子查询。

是否还有一种方法可以应用相同的代码来返回多行?我想显示所有stockclientID的结果,而不仅仅是1。

Select 

(Select distinct SName FROM classifiedadmin.readOnly_CoreData) as SupplierName,

(SELECT COUNT(*) FROM classifiedadmin.readOnly_CoreData where Successful= 1 and StockClientID=3) as TotalLive,

(Select count(*) FROM classifiedadmin.readOnly_CoreData where length(DetailDesc) > 200 and StockClientID= 3) as DescriptionOver200,

(select count(*) from classifiedadmin.readOnly_CoreData where busruleViolated=1 and StockClientID = 3) as BusinessRuleViolated,

(Select count(*) FROM classifiedadmin.readOnly_CoreData where StockClientID = 3
and Successful=0
and busruleviolated=0
and Edition not like '%auto%'
and Edition not like '%shift%'
and Edition not like '%tronic%'
and Edition not like '%van%'
and Edition not like '%DSG%') as UnadvertisedManual;

1 个答案:

答案 0 :(得分:0)

SELECT  MIN(sname),
        SUM(Successful= 1),
        SUM(length(DetailDesc) > 200),
        SUM(busruleViolated=1),
        SUM(
        Successful=0
        and busruleviolated=0
        and Edition not like '%auto%'
        and Edition not like '%shift%'
        and Edition not like '%tronic%'
        and Edition not like '%van%'
        and Edition not like '%DSG%'
        )
FROM    classifiedadmin.readOnly_CoreData
WHERE   StockClientID = 3

请注意,如果给定的SELECT DISINCT sname内有多个sname,则stockclientid的当前查询将失败。