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
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
相关文章推荐
- mysql中sql语句中常见的group_concat()函数意思以及用法,oracle中与其一样的功能函数是wmsys.wm_concat()
- MySQL 连接字符串函数 CONCAT() CONCAT_WS() GROUP_CONCAT()
- C++ 连接mysql 数据库,以及query语句
- mysql 查询一个字段,生成连接字符串输出 统计函数 group_concat concat concat_ws
- mysql GROUP_CONCAT 以及 其逆过程
- mysql GROUP_CONCAT 排序
- MySQL中group_concat()函数的排序方法
- MySQL的字符串连接函数CONCAT, CONCAT_WS,GROUP_CONTACT
- 浅谈MySQL中group_concat()函数的排序方法
- MySQL的行转列、列转行、连接字符串 concat、concat_ws、group_concat函数用法
- MySQL如何建索引以及利用索引优化ORDER BY排序语句
- MySQL如何建索引以及利用索引优化ORDER BY排序语句
- 关于Mysql group_concat的应用(把相同ID的VAL用字符'/'连接起来)
- MySQL中GROUP_CONCAT中的排序和替换
- web测试常用python代码——mysql连接以及语句执行
- MySQL的行转列、列转行、连接字符串 concat、concat_ws、group_concat函数用法
- 连接数据库的步骤SQL Server 与mysql 以及一些数据库查询语句
- MySQL的行转列、列转行、连接字符串 concat、concat_ws、group_concat函数用法
- mysql group_concat 分组,多字段统一排序及输出数据到java
- 浅析MySQL中concat以及group_concat的使用