Home > Backend Development > PHP Tutorial > mysql order排序时可否用其它表中的数据来排序

mysql order排序时可否用其它表中的数据来排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 13:24:50
Original
980 people have browsed it

mysql order排序时能否用其它表中的数据来排序?
情况是这样的
A表字段user dt dg
  a 15 10
  b 17 15
B表字段user title jh
  a xxx 1
  b xxx 0
  b xxxxx 0
  a xxxxx 0
  b xxxxx 1

想要实现的是,根据A表中的user与B表中的user值得出 B表中的有多少与该user相关的数据,按数据条数从多到少排序,然后输出结果为
user dt jh B表中的条数
b 17 1 3
a 15 1 2

 

------解决方案--------------------
select A.*, T.jh from A, (select user,count(*) as jh from B) T where A.user=T.user order by T.jh desc
------解决方案--------------------
select a.user,a.dt,sum(b.jh) as jh,count(b.user) as counter from a inner join b on a.user=b.user group by b.user order by counter desc;

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Switch user
From 1970-01-01 08:00:00
0
0
0
User login
From 1970-01-01 08:00:00
0
0
0
Delete user to get id
From 1970-01-01 08:00:00
0
0
0
Translate: Laravel: Get Sanctum token by user ID
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template