Keren Keren - 3 months ago 6
MySQL Question

subquery doesn't seem to work

mysql query:

`SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`,` `a.`lastreplied`,`

a.`type`, b.`created_by`, b.`message`, c.`isread`


FROM `jos_social_conversations` AS `a`

LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id
and party.user_id = '602'

INNER JOIN `jos_social_conversations_message` AS `b` ON
`a`.`id` = `b`.`conversation_id`

INNER JOIN `jos_social_conversations_message_maps` AS `c`
ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`



INNER JOIN
(select cm.`conversation_id`, max(cm.`message_id`) as `message_id` from
`jos_social_conversations_message_maps` as cm

inner join `jos_social_conversations_message` as bm
on cm.`message_id` = bm.`id`

LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id`

AND `bus`.`target_id` = '602'

WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg


FROM jos_social_conversations_message_maps as maps WHERE a.id = maps.conversation_id AND
maps.isread = 0 AND maps.user_id = '602') AND `cm`.`state` = '1' and `bus`.`id` IS NULL
group by cm.`conversation_id`) as x ON c.`message_id` = x.`message_id`



LEFT JOIN `jos_social_block_users`
as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'

WHERE `c`.`user_id` = '602'



AND bus.`id` IS NULL AND `c`.`state` = '1'


AND this gives error like:


1054 - Unknown column 'a.id' in 'where clause'




subquery from above query as below:

AND(SELECT count(isread) AS newMsg FROM jos_social_conversations_message_maps WHERE conversation_id = 3 AND isread = 0 AND user_id = '602')


Current output:
enter image description here

Expected output:

There should be another column called
'newMsg'
displaying total count of
'isread'
column which has
'0'
as value for each id..

MJH MJH
Answer

Here's my formatted version:

SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON `a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c` ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN  (select cm.`conversation_id`, max(cm.`message_id`) as `message_id`
             from `jos_social_conversations_message_maps` as cm
             inner join `jos_social_conversations_message` as bm on cm.`message_id` = bm.`id`
             LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id` AND `bus`.`target_id` = '602'
             WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg 
                                              FROM jos_social_conversations_message_maps
                                              WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
                                             )
                                          AND `cm`.`state` = '1'
                                          and `bus`.`id` IS NULL
             group by cm.`conversation_id`
            ) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users` as bus  ON a.`created_by` = bus.`user_id`  AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
  AND bus.`id` IS NULL AND `c`.`state` = '1'


Your subquery is part of the WHERE clause, so it will not return another column. Perhaps you are lookig for something like this:

SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`,
       (SELECT count(isread)
        FROM jos_social_conversations_message_maps
        WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
       ) AS newMsg 
FROM `jos_social_conversations` AS `a`
{remainder removed for brevity}