我有一個查詢mysql查詢子查詢循環?
SELECT
count(product) as amount,
product,
sum(price) AS price
FROM `products`
WHERE
brid = 'broker'
AND
cancelled is null
GROUP BY product
WITH ROLLUP
是否可以查詢表得到一個經紀人ID,然後爲每個代理運行查詢,上面寫的是1個查詢?
幾乎像:
SELECT brid FROM membership
THEN
SELECT
count(product) as amount,
product,
sum(price) AS price
FROM `products`
WHERE
brid = membership.brid
AND
cancelled is null
GROUP BY product
WITH ROLLUP
THEN
SELECT NEXT brid
這可能嗎?我知道如何在PHP中做到這一點,但我更喜歡1查詢可以創建一個數組而不是每個查詢噸。
謝謝 亞當。
在PhpMyAdmin中測試並得到---#1221 - 錯誤地使用了CUBE/ROLLUP和ORDER BY –
你是對的,'WITH ROLLUP'排除了'ORDER BY'的使用並且已經爲你排序 - 所以你可以跳過ORDER BY子句。更新。 – PinnyM
謝謝!現在正常工作! –