【发布时间】: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 粘贴完整代码并尽可能格式化