我正在从多个表中获取数据。并试图解决这个问题
最后我有两个查询
1) 获取用户消息的相关数据。
2) 获取所有评论及其详细信息。
我想将这两个查询组合在一个查询中。
查询 1)
SELECT user_messages.messageid,user_messages.message,
user_messages.sentby,user_messages.visibility,
GROUP_CONCAT( post_images.image_id SEPARATOR ';')
AS `POST_IMG_ID`,
GROUP_CONCAT( post_images.small_pic_path SEPARATOR ';')
AS 'POST_IMG_PATH',
(
SELECT count(*) FROM likes
WHERE element_id=user_messages.messageid
)AS 'TOTAL_LIKES',
smsusers.fname as 'SENTBY_FNAME',
smsusers.lname as 'SENTBY_LNAME',
profile_pic.small_pic_path as 'SENTBY_SMALL_PIC_PATH'
FROM user_messages
INNER JOIN smsusers ON
user_messages.SENTBY = smsusers.id
INNER JOIN profile_pic ON
user_messages.SENTBY = profile_pic.userid
left outer JOIN post_images ON
user_messages.messageid=post_images.messageid
WHERE user_messages.userid='1'
GROUP BY user_messages.messageid
order by user_messages.adddate
它的工作链接是Sql Fiddle
查询 2)
SELECT comments.comment_id, comments.comment, comments.comment_date,
smsusers.fname ,smsusers.lname,profile_pic.small_pic_path
FROM comments, smsusers , profile_pic , user_messages
WHERE user_messages.messageid = 'm1'
AND user_messages.userid = smsusers.id
AND smsusers.id = profile_pic.userid
ORDER BY comment_date
LIMIT 0, 10;
工作链接是Sql Fiddle