SELECT
l.lid,
l.title,
l.uid,
l.touid,
l.listencount,
MAX(lr.creationdate) AS lrcreationdate,
l.creationdate,
l.sort,
l.status,
1 AS payinfo,
l.sid,
MIN(lr.content) AS content,
MIN(CAST(lr.duration AS CHAR)) AS duration
FROM
gxb_live l,
gxb_liveresource lr
WHERE
l.deleted = 0
AND l.ispublic = 0
AND l.lid IN (lr.liveid)
AND l.touid = lr.uid
AND lr.deleted = 0
AND lr.type = 2
AND l.status = 2
GROUP BY l.lid
ORDER BY lrcreationdate DESC
如上代码,content为varchar型,用min()函数后可以按照lr表的自增id排序,但是原因是duration是int型而且是数字,min出来只能是最小值而非最小id的值,转型也不行。
所以要想取到duration的最小id的值并且和content对应有什么简单易行的好办法?
l.lid,
l.title,
l.uid,
l.touid,
l.listencount,
MAX(lr.creationdate) AS lrcreationdate,
l.creationdate,
l.sort,
l.status,
1 AS payinfo,
l.sid,
MIN(lr.content) AS content,
MIN(CAST(lr.duration AS CHAR)) AS duration
FROM
gxb_live l,
gxb_liveresource lr
WHERE
l.deleted = 0
AND l.ispublic = 0
AND l.lid IN (lr.liveid)
AND l.touid = lr.uid
AND lr.deleted = 0
AND lr.type = 2
AND l.status = 2
GROUP BY l.lid
ORDER BY lrcreationdate DESC
如上代码,content为varchar型,用min()函数后可以按照lr表的自增id排序,但是原因是duration是int型而且是数字,min出来只能是最小值而非最小id的值,转型也不行。
所以要想取到duration的最小id的值并且和content对应有什么简单易行的好办法?
解决方案
20
在结果集外层分组,已经没有两个表的概念了 select min() from ( your sql) group by id ;
20
还是lrcreationdate取最大值就行了
SELECT l.lid, l.title, l.uid, l.touid, l.listencount, MAX(lr2.creationdate) AS lrcreationdate, l.creationdate, l.sort, l.status, 1 AS payinfo, l.sid, lr.content, CAST(lr.duration AS CHAR) AS duration FROM gxb_live l, gxb_liveresource lr, gxb_liveresource lr2 WHERE l.deleted = 0 AND l.ispublic = 0 AND l.lid IN (lr.liveid) AND l.touid = lr.uid AND lr.deleted = 0 AND lr.type = 2 AND l.status = 2 and not exists(select 1 from gxb_liveresource where deleted=lr.deleted and uid=lr.uid and liveid=lr.liveid and type=lr.type and id<lr.id) AND l.lid IN (lr2.liveid) AND l.touid = lr2.uid AND lr2.deleted = 0 AND lr2.type = 2 group by l.lid ORDER BY lrcreationdate DESC