任务是获取有两个以上不同特殊代码的订单代码列表, 现在我已经有了这个查询
SELECT
o.id AS iOrderId,
o.code AS sOrderCode,
oa.pnr AS sSpecialCode
FROM
order_air AS oa
JOIN
orders AS o ON 1
AND oa.order_id = o.id
WHERE 1
AND o.status = 11
AND oa.issued = 1
AND oa.change_status != 0
AND oa.issued_date BETWEEN NOW() - INTERVAL 12 MONTH AND NOW()
GROUP BY o.id
HAVING COUNT(*) >= 2
LIMIT 20
它会让我回头
一切都很好,但是这个查询不检查不同的特殊代码,只返回所有超过2个特殊代码的订单代码。 我已经尝试过调查IF以保存以前的orderid,然后通过它进行检查,但我没有意识到如何将以前的id存储到类似@prev的内容中,所以这对我没有帮助,任何想法如何过滤这个队列吧?
答案 0 :(得分:1)
我认为这就是你要做的事。
SELECT
o.id AS iOrderId,
o.code AS sOrderCode
FROM
orders AS o
JOIN
(SELECT order_id, COUNT(DISTINCT pnr) AS cnt
FROM order_air
WHERE issued = 1
AND change_status != 0
AND issued_date BETWEEN NOW() - INTERVAL 12 MONTH AND NOW()
GROUP BY order_id) oa_count
ON o.id = oa_count.order_id
WHERE oa_count.cnt >= 2
AND o.status = 11
LIMIT 20
答案 1 :(得分:0)
JOIN
orders AS o ON 1
AND oa.order_id = o.id
AND something < something else