我正在从多个表中获取数据,所有其他数据均已正确获取,但 post_images 的 IMAGE_POST_ID POST_IMG_PATH字段未根据 user_messages 的 messageid 显示。
我的桌子是
1)post_images
| IMAGE_ID | SMALL_PIC_PATH | LARGE_PIC_PATH | ADDDATE | VISIBILITY | USERID | MESSAGEID |
---------------------------------------------------------------------------------------------------------------------
| i_i1 | /post/small/a.jpg | /post/img/a.jpg | January, 01 2012 00:00:00+0000 | public | 2 | m1 |
| i_i2 | /post/small/b.jpg | /post/img/b.jpg | January, 01 2012 00:00:00+0000 | public | 2 | m1 |
| i_i3 | /post/small/c.jpg | /post/img/c.jpg | January, 01 2012 00:00:00+0000 | public | 2 | m1 |
| i_i4 | /post/small/d.jpg | /post/img/d.jpg | January, 01 2012 00:00:00+0000 | public | 2 | m1 |
| i_i5 | /post/small/e.jpg | /post/img/e.jpg | January, 01 2012 00:00:00+0000 | public | 3 | m2 |
| i_i6 | /post/small/f.jpg | /post/img/f.jpg | January, 01 2012 00:00:00+0000 | public | 3 | m2 |
2)user_messages
| MESSAGEID | MESSAGE | ADDDATE | SENTBY | VISIBILITY | USERID |
----------------------------------------------------------------------------
| m1 | Hello how are you | 23-2-2012 | 2 | public | 1 |
| m2 | Hello | 23-2-2012 | 3 | public | 1 |
| m3 | Hiiiiiii | 23-2-2012 | 4 | public | 1 |
我想获取相关消息 id 的所有 image_id 和 small_pic_path。但是通过我的查询,它显示了所有带有 messageid 的 image_id small_pic_path。
这是我的查询
SELECT user_messages.messageid,user_messages.message,
user_messages.sentby,user_messages.visibility,
(
SELECT GROUP_CONCAT( `post_images`.`image_id` SEPARATOR ';')
FROM `post_images`
JOIN user_messages
ON `post_images`.`messageid` =user_messages.messageid
) AS `Image_post_id`,
(
SELECT GROUP_CONCAT( `post_images`.`small_pic_path` SEPARATOR ';')
FROM `post_images`
JOIN user_messages
ON `post_images`.`messageid` =user_messages.messageid
) AS Post_Img_path,
(
SELECT count(*) FROM likes
WHERE element_id=user_messages.messageid
)AS Total_Likes
, smsusers.fname as Msg_sender_fname,
smsusers.lname as Msg_sender_lname,
profile_pic.small_pic_path as Msg_sender_pic
FROM user_messages
INNER JOIN smsusers ON
user_messages.SENTBY = smsusers.id
INNER JOIN profile_pic ON
user_messages.SENTBY = profile_pic.userid
WHERE user_messages.userid='1';
这是我的查询链接