【问题标题】:subquery doesn't seem to work子查询似乎不起作用
【发布时间】:2017-01-15 20:02:07
【问题描述】:

mysql查询:

`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'  

这会产生如下错误:

1054 - “where 子句”中的未知列“a.id”

来自上述查询的子查询如下:

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

电流输出:

预期输出:

应该有另一个名为'newMsg' 的列显示'isread' 列的总数,其中'0' 作为每个ID 的值..

【问题讨论】:

  • 您可以花一些精力来格式化查询吗?
  • @GordonLinoff,为简洁起见删除了一些代码行..
  • 您的 SELECT 列表包含八列。是什么让你认为应该有另一个?请按照@Gordon 的建议发布完整的查询,格式应便于阅读。
  • @GordonLinoff 粘贴完整代码并尽可能格式化

标签: mysql subquery


【解决方案1】:

这是我的格式化版本:

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'


您的子查询是 WHERE 子句的一部分,因此它不会返回另一列。也许你正在寻找这样的东西:

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}

【讨论】:

  • 感谢答案按预期工作.. 是的,现在我知道 mysql 的格式化版本..再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多