2017-01-19 28 views
-1

喜我的问题左连接SQL查询

contact_message表(1个数据)
-id
-messageid
-message
-date

列表contact_message数据
MySQL的左连接的SQL查询列表中的解决方案

Array 
(
    [0] => Array 
     (
      [id] => 5 
      [messageid] => 1 
      [message] => A new e-mail 
      [date] => 2017-01-18 
     ) 

) 

contact_reply表(2数据)
-id
-messageid
-reply
-date
列表contact_reply数据

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [messageid] => 5 
      [reply] => Thanks I save mail adress 
      [date] => 2017-01-18 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [messageid] => 5 
      [reply] => Sorry What is your name ? 
      [date] => 2017-01-18 
     ) 

) 


SQL查询

SELECT im.message,im.date,ic.reply,ic.date 
     from contact_message as im 
     left join contact_reply as ic 
     on im.id=ic.messageid 
     where im.messageid=:siral 


结果

Array 
(
    [0] => Array 
     (
      [message] => A new e-mail 
      [date] => 2017-01-18 
      [reply] => Thanks I save mail adress İSMAİL ÇİLOĞLU 
     ) 

    [1] => Array 
     (
      [message] => A new e-mail 
      [date] => 2017-01-18 
      [reply] => Sorry What is your name ? 
     ) 

) 

正常[1] INDIS消息null值,但写的[0] INDIS [消息]值
我想结果

Array 
    (
     [0] => Array 
      (
       [message] => A new e-mail 
       [date] => 2017-01-18 
       [reply] => Thanks I save mail adress İSMAİL ÇİLOĞLU 
      ) 

     [1] => Array 
      (
       [message] =>null 
       [date] => 2017-01-18 
       [reply] => Sorry What is your name ? 
      ) 

    ) 
+0

对不起,但我不确定你在问什么。 –

+0

joe C. sql查询结果[1]消息列通常为空但写[0] indis mesage列值:( –

+0

对不起,我的中间和不好:/ –

回答

0

你的表结构似乎是不正确

Contact_message table (1 data) 
-id 
-messageid 
-message 
-date 

contact_reply表(2数据)

-id 
-contact_message_id // Foreign Key of table1 
-reply 
-date 

Sql查询

SELECT im.message,im.date,ic.reply,ic.date 
    from contact_message as im 
    left join contact_reply as ic 
    on im.id=ic.contact_message_id 
    where im.messageid=:siral 
+0

遗憾相同的结果:( –

+0

阵列 ( [0] =>数组 ( [消息] =>新的电子邮件 [日期] => 2017年1月18日 [回复] =>由于我保存邮件ADRESS ISMAILÇİLOĞLU ) [1] =>数组 ( [消息] =>新的电子邮件 [日期] => 2017年1月18日 [reply] =>对不起你叫什么名字? ) ) –

+0

您期待什么输出? –