查询以获取用户的详细信息

时间:2013-02-23 15:04:58

标签: mysql sql select join

这是我的query,它会生成以下表格

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 Small_pic_path,
(
  SELECT count(*) FROM likes
  WHERE element_id=user_messages.messageid
  )AS Total_Likes

FROM user_messages
WHERE user_messages.userid='1';

Sql table

这里我想要包含 sentby 的详细信息,这是smsusers表中的id。 我想要来自smsusers表的详细信息fname,lname和来自profile_pic表的small_pic_path。 如何获取详细信息。

1 个答案:

答案 0 :(得分:1)

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 Small_pic_path,
        (
            SELECT count(*) 
            FROM likes
            WHERE element_id=user_messages.messageid
        ) AS Total_Likes, 
        smsusers.*  -- select the columns you want to show
FROM    user_messages
        INNER JOIN smsusers 
        ON user_messages.SENTBY = smsusers.id
WHERE   user_messages.userid= '1';

要进一步了解联接,请访问以下链接: