开发者

Subselects or something more elegant?

It's difficult to explain what exatly i'm trying to do, so heres the query that allready works:

SELECT
(SELECT COUNT(cars) '. HUGE_FROM_LIST .' '. HUGE_WHERE_QUERY .' AND cars > 0 ) AS cars,
(SELECT COUNT(houses) '. HUGE_FROM_LIST .' '. HUGE_WHERE_QUERY .' AND houses_type = 8 ) AS houses8,
...

This way i get two results, telling me how many cars and houses of type 8 there are. I dont wont to do one query for every result, so i tried subselects.

I do subselects, so the single querys o开发者_如何学Pythonnly depend on their own WHERES. Problem is that HUGE_FROM_LIST and HUGE_WHERE_QUERY are repeated every time and the whole thing gets really big.

Is there something more elegant?


SELECT
    COUNT(IF(cars > 0, TRUE, NULL)) AS cars,
    COUNT(IF(houses_type = 8, TRUE, NULL)) AS houses8,
    ...
'. HUGE_FROM_LIST .'
'. HUGE_WHERE_QUERY


I'm not sure why you don't want to do multiple queries, but no matter how you do it... if you have SELECT criteria (logic) that will be repeated in many different places then it may be a good idea to encapsulate that logic into a single view. You can then simply refer to the view when you need to get at that data using that logic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜