2017-04-23 113 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 

也尽量不使用反引号可读性。

如果这不是您想要的,请编辑您的问题并添加示例数据和预期输出。