我有这样的查询:
SELECT
oh.date_added AS date_shipped,
o.date_added
FROM
".DB_PREFIX."order o , ".DB_PREFIX."order_history oh
WHERE
o.order_id = oh.order_id
AND oh.order_status_id = 3
我想要做的只是在有X
行或特定日期的行时显示结果。
因此,如果X=5
有2013-04-0
1中的三个条目,则它们将不显示,但如果>= 5
中有九个条目(或任何2013-04-02
)然后这些结果将包含在查询中。
一旦满足要求,我希望查询限制匹配日期中的那一组行。
有谁知道我怎么做到这一点?
答案 0 :(得分:2)
您可以添加嵌套SELECT
来计算这些:
SELECT
oh.date_added AS date_shipped,
o.date_added
FROM
".DB_PREFIX."order o , ".DB_PREFIX."order_history oh
WHERE
o.order_id = oh.order_id
AND oh.order_status_id = 3
AND (SELECT COUNT(*) FROM ".DB_PREFIX."order o2
WHERE o2.date_added>"2013-04-01") > 5
或者按照
的方式连接表格SELECT
o.date,
COUNT(*) cnt
FROM
order o
JOIN order o2 ON o2.date = o.date AND o2.id != o.id
WHERE
your conditions
GROUP BY o2.id
HAVING cnt > 5