2017-04-23 122 views
0

MySQL查詢:MYSQL計數查詢錯誤

SELECT `message`.`id` as `message_id`, 
`pet_info`.`id` as `pet_id`, 
`pet_info`.`pet_hidenum` as `hidenum`, 
`lostpets`.`pet_lost_date` as `pet_lost_date`, 
`lostpets`.`type` as `status`, 
`pet_images`.`img` as `img`, 
COUNT(SELECT * FROM `message` WHERE `message`.`status` = 'not seen') as unread 
FROM `message` 
LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id` 
LEFT JOIN `pet_images` ON `pet_images`.`petid` = `message`.`pet_id` 
LEFT JOIN `lostpets` ON `lostpets`.`petid` = `message`.`pet_id` 

錯誤:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `message` WHERE `message`.`status` = 'not seen') as unread FROM `m' at line 1 

請幫我在哪裏,是在此查詢錯誤?以及如何解決此錯誤?

+0

你得到的錯誤是什麼?發佈錯誤堆棧。 –

+0

@AbdullahWasi問題已更新 –

回答

0

不知道你的意圖是什麼,但對於錯誤,你要應用的子查詢中的計數功能:通過使用標準的別名和標識的名字,因爲反引號阻礙

select message.id as message_id, 
    pet_info.id as pet_id, 
    pet_info.pet_hidenum as hidenum, 
    lostpets.pet_lost_date as pet_lost_date, 
    lostpets.type as status, 
    pet_images.img as img, 
    (
     select COUNT(*) 
     from message 
     where message.status = 'not seen' 
     ) as unread 
from message 
left join pet_info on pet_info.id = message.pet_id 
left join pet_images on pet_images.petid = message.pet_id 
left join lostpets on lostpets.petid = message.pet_id 

也儘量不使用反引號可讀性。

如果這不是您想要的,請編輯您的問題並添加示例數據和預期輸出。