mysql 多表查询并排序

mysql 多表查询并排序,第1张

select t.authorid,t.author,COUNT(t.authorid) AS authornum from(select authorid,author,time from 表1union allselect authorid,author,time from 表2) twhere t.time >$time GROUP BY t.authorid,t.author ORDER BY authornum DESC

基本就这样。表名你换成你自己的表名,我这里分别用表1和表2表示的

先Union,再Order by.

select *

from t1

where 名字='王艳'

union all

select *

from t2

where 名字='王艳'

.

.

.

order by 时间

如果,这两张表id,userid可作关联条件,可将两张表关联查询。

如:

select a.id,a.userid,a.title,a.time,b.id,b.userid,b.title,b.time from a,b

where a.id=b.id and a.userid=b.userid

order by a.time


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9904867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存