這是我的全部查詢:Mysql的GROUP BY和ORDER BY DESC
SELECT * FROM `clientgroupassign`
LEFT JOIN `clients` ON `clientgroupassign`.clientId = `clients`.clientId
LEFT JOIN `users` ON `carerId` = `userId`
LEFT JOIN
(SELECT * FROM
(SELECT * FROM `contacts` WHERE `contactGroup` = 4 ORDER BY `contactId` DESC)
as `contacts`
GROUP BY (`contactClientId`)
) AS `contacts` ON `contactClientId` = `clients`.clientId
WHERE groupId = 4
ORDER BY `clients`.clientId
有一個與第三聯接導致腳本執行約1分鐘的一個問題。當我在PMA中分開運行它時:
SELECT * FROM (SELECT * FROM `contacts` WHERE `contactGroup` = 4 ORDER BY `contactId` DESC) AS `contacts` GROUP BY (`contactClientId`)
它仍然會很長時間才能執行。
我想要的是得到一個,最後添加從contacts
排爲每一個客戶是誰在組4(客戶端可以在不同的羣體)。
謝謝。
什麼'DESCRIBE' /'EXPLAIN'顯示該查詢? – BlitZ
另外,從那裏刪除'SELECT * ...'。選擇特定的列。它可能會增加開銷。 'SELECT *'可能是一個真正的速度殺手。 – BlitZ