2016-09-08 5 views
0

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

電流出力: enter image description here

予想される出力:

各IDの値として'0'を持って'isread'列の合計数を表示する'newMsg'と呼ばれる別の列があるはずです。..

+0

あなたはクエリを書式設定するのに少し力を入れることができますか? –

+0

@ GordonLinoff、簡潔さのためにコード行を削除しました.. – 112233

+0

あなたのSELECTリストには8列が含まれています。あなたは別のものがなければならないと思いますか? @Gordonが示唆しているように、完全なクエリを、読めるように書いてください。 – MJH

答えて

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} 
+0

答えが期待どおりに機能してくれました.. ANd yeah今、私はmysql..thanksのフォーマット版をもう一度知っています – 112233