简体   繁体   English

按另一个表的结果排序

[英]Order by result from another tables

HI i have skills tables as below 嗨,我的技能表如下

在此处输入图片说明

Various user add skills to their profile. 各种用户向其个人资料添加技能。 Now i want to list all the skills decreasing order of their uses. 现在,我想列出所有技能的降序使用。 Like as below 如下图

Php(10) , ASP (5) , Perl(1) PHP(10),ASP(5),Perl(1)

Its means 10 user added php as their skill, 5 user ASP etc. 它的意思是10位用户添加了php作为技能,5位用户ASP等等。

I have stored the skills in user table in skills column with comma separated 我已将技能存储在用户表的技能列中,并以逗号分隔

在此处输入图片说明

Try this: 尝试这个:

select id, name
from (
    select *, (select sum(1) from user u where find_in_set(s.id, u.skills)) as cnt
    from skills s
) t
order by cnt desc
-- limit 20

Assuming you have another table/relation to represent user_skills, that contains (for example) user_id and skill_id foreign keys, then you'd want to join this to the skills table and group the results similar to this: 假设您有另一个表/关系来表示user_skills,其中包含(例如)user_id和skill_id外键,那么您想要将此表/关系加入到Skills表并将结果分组如下:

select name, count(skill_id) as ranking
from skills join user_skills on skills.id = user_skills.skill_id
group by name
order by count(skill_id) desc, name asc;

It excludes skills which have not been selected by any users. 它不包括任何用户未选择的技能。 If you wanted to include those too, change the join to a left join. 如果您也想包括这些,请将联接更改为左联接。

Edit: With the original question updated to include the definition of the users table, then perhaps this would suffice. 编辑:将原始问题更新为包括users表的定义,那么也许就足够了。 Again you would need a left join to include skills that no users had picked. 同样,您将需要左联接来包括没有用户选择的技能。

select skills.name, count(users.id) as qty
from skills join users 
on locate(concat(',', skills.id, ','), concat(',', users.skills, ',')) > 0
group by skills.name
order by count(users.id) desc, skills.name asc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM