您的位置:首页 > 数据库 > MySQL

mysql语句group concat 语句,以及连接排序

2014-06-23 15:04 316 查看
SELECT

  p_result.id,

  p_result.username,

  p_result.userid,

  p_result.nc,

  p_result.icon,

  p_result.time,

  p_result.content,

  i.small_pics,

  i.big_pics,

  IF(

    ag_table.ag_count IS NULL,

    0,

    ag_table.ag_count

  ) AS dianz,

  agreements_table.dianz_userids AS dianz_userids,

  agreements_table.dianz_users AS dianz_users,

  IF(

    c_table.c_count IS NULL,

    0,

    c_table.c_count

  ) AS pinglun,

  IF(

    re_table.re_count IS NULL,

    0,

    re_table.re_count

  ) AS zhuanfa,

  comments_table.comments,

  p_result.evaluation,

  p_result.explosure,

  p_result.status

FROM

  (SELECT

    p.id,

    p.content AS content,

    p.create_time AS TIME,

    u.id AS userid,

    u.username,

    u.nicheng AS nc,

    CONCAT(

      'http://192.168.15.64:8090/epservice/image/',

      u.photo_path,

      '.144x144.jpg'

    ) AS icon,

    p.evaluation,

    p.explosure,

    p.status

  FROM

    epimap.ep_living_posts p,

    epimap.ep_user_info u

  WHERE p.userid = u.id

    AND p.province = '河南省'

    AND p.city = '郑州') AS p_result

  LEFT JOIN

    (SELECT

      id,

      postid,

      GROUP_CONCAT(

        CONCAT(

          'http://192.168.15.64:8090/epservice/',

          path

        ) SEPARATOR '#'

      ) AS big_pics,

      GROUP_CONCAT(

        CONCAT(

          'http://192.168.15.64:8090/epservice/',

          path,

          '.240x240.jpg'

        ) SEPARATOR '#'

      ) AS small_pics

    FROM

      ep_living_images

    GROUP BY postid) AS i

    ON i.postid = p_result.id

  LEFT JOIN

    (SELECT

      postid,

      COUNT(*) AS c_count

    FROM

      epimap.ep_living_comments c

    GROUP BY postid) AS c_table

    ON p_result.id = c_table.postid

  LEFT JOIN

    (SELECT

      postid,

      COUNT(*) AS re_count

    FROM

      epimap.ep_living_reposts re

    GROUP BY postid) AS re_table

    ON p_result.id = re_table.postid

  LEFT JOIN

    (SELECT

      postid,

      COUNT(*) AS ag_count

    FROM

      epimap.ep_living_agreements ag

    GROUP BY postid) AS ag_table

    ON p_result.id = ag_table.postid

  LEFT JOIN

    (SELECT

      postid,

      GROUP_CONCAT(ep_user_info.id SEPARATOR '#') AS dianz_userids,

      GROUP_CONCAT(username SEPARATOR '#') AS dianz_users

    FROM

      ep_living_agreements,

      ep_user_info

    WHERE ep_living_agreements.userid = ep_user_info.id

    GROUP BY postid

    ORDER BY ep_living_agreements.`agreetime` DESC) AS agreements_table

    ON agreements_table.postid = p_result.id

  LEFT JOIN

    (SELECT

      postid,

      username,

      GROUP_CONCAT(

        CONCAT(

          ep_user_info.id,

          '#',

          ep_living_comments.id,

          '#',

          username,

          '#',

          ep_living_comments.create_time,

          '#',

          content

        ) ORDER BY ep_living_comments.id DESC SEPARATOR '╬'

      ) AS comments

    FROM

      ep_living_comments,

      ep_user_info

    WHERE ep_living_comments.userid = ep_user_info.id

    GROUP BY postid

    ORDER BY ep_living_comments.`create_time` DESC) AS comments_table

    ON comments_table.postid = p_result.id

ORDER BY p_result.time DESC
LIMIT 0, 6

评论列值为:

393#864#wantingting#2014-06-23 14:48#排序成功╬393#863#wantingting#2014-06-23 14:48#排序2╬393#862#wantingting#2014-06-23 14:47#排序1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: