我想從另一個SELECT內部的相同查詢中獲取信息,但是我不太確定如何獲得我所需的字段。MySQL查詢COUNT輸出
SELECT t.`id`, t.`title`, t.`author`, t.`content`, ctitle, cid, comments
FROM `tutorials` AS `t`,
(
SELECT tc.`id` as `cid`, tc.`title` as `ctitle`
FROM `tutorial_categories` AS `tc`
WHERE `title` LIKE '%category title'
) AS `c`,
(
SELECT COUNT(com.`id`) as `comments`
FROM `tutorial_comments` AS `com`
WHERE `tutorial_id` = c.cid
) as `comments`
WHERE t.`category` = c.`cid` AND t.`status` = '1'
ORDER BY `id` ASC
我試圖從tutorial_categories
獲取ID和tutorial_comments
使用它。所有我想要做的最終輸出是獲得每個教程列出的評論數量。
乾杯,
雅各
現在只顯示有評論的結果,而不是每個類別的所有結果。 –
我很抱歉,但我沒有明白你的意思。您可以嘗試左連接以顯示那些沒有評論的教程,並且您可以按類別排序以對結果集進行排序。順便說一句,我想你可以嘗試@erwin的腳本。 –