简体   繁体   English

MySQL同时选择多个id,可选id with php

[英]MySQL select multiple id at the same time with optional id with php

I would like to complicate this request by changing the scenario.我想通过改变场景来使这个请求复杂化。 Here is the link to the original request.这是原始请求的链接。 Here is the link to the original request. 这是原始请求的链接。

I have the following MySQL table called skills.我有以下 MySQL 表称为技能。

id ID idUser用户名 idSkill技能
1 1 4 4 1 1
2 2 8 8 4 4
3 3 8 8 9 9
4 4 13 13 9 9
5 5 18 18 2 2
6 6 22 22 1 1
7 7 27 27 2 2
8 8 32 32 4 4
9 9 11 11 2 2
10 10 32 32 9 9
10 10 32 32 7 7

I need to select, for example, all idUsers that have idSkill 4 and 9 at the same time (mandatory skills).例如,我需要选择同时具有 idSkill 4 和 9(强制技能)的所有 idUser。

But I would like to have the possibility to search by optional idSkills (if any).但我希望有可能通过可选的 idSkills(如果有)进行搜索。

Mandatory skills are 9 and 4必修技能是 9 和 4

Optional skill is 7可选技能为7

The result would be idUser 32.结果将是 idUser 32。

I thought of this query:我想到了这个查询:

SELECT id, idUser, idSkill FROM skills WHERE idSkill IN (9,4,7) GROUP BY idUser HAVING (idSkill IN (9,4))

But it clearly does not work.但它显然不起作用。

Many thanks非常感谢

You can do it with aggregation and RANK() window function.您可以使用聚合和RANK()窗口函数来实现。

This query:这个查询:

SELECT idUser
FROM skills 
WHERE idSkill IN (9, 4, 7) 
GROUP BY idUser 
HAVING SUM(idSkill IN (9, 4)) = 2 -- for the 2 mandatory skills

returns all the users with at least the 2 mandatory skills 9 and 4.返回至少具有 2 项强制性技能 9 和 4 的所有用户。

If you use an ORDER BY clause with LIMIT like this:如果像这样使用带有LIMITORDER BY子句:

SELECT idUser
FROM skills 
WHERE idSkill IN (9, 4, 7) 
GROUP BY idUser 
HAVING SUM(idSkill IN (9, 4)) = 2 -- for the 2 mandatory skills
ORDER BY COUNT(*) DESC
LIMIT 1

you will get from all the users with at least the 2 mandatory skills 9 and 4, only 1 user: the one with the largest number of skills (mandatory and optional).您将从所有拥有至少 2 个强制技能 9 和 4 的用户那里获得,只有 1 个用户:拥有最多技能(强制和可选)的用户。

If you want ties returned, use RANK() window function:如果您想返回RANK() ,请使用RANK()窗口函数:

SELECT idUser
FROM (
  SELECT idUser, RANK() OVER (ORDER BY COUNT(*) DESC) rnk
  FROM skills 
  WHERE idSkill IN (9, 4, 7) 
  GROUP BY idUser 
  HAVING SUM(idSkill IN (9, 4)) = 2 -- for the 2 mandatory skills
) t
WHERE rnk = 1

See the demo .请参阅演示

Your answer will be a simple query.您的回答将是一个简单的查询。

select * from (select distinct b.idUser, (select 1 from skill a where a.idUser=b.idUser and a.idSkill=4) 'four', (select 1 from skill a where a.idUser=b.idUser and a.idSkill=9) 'nine', (select 1 from skill a where a.idUser=b.idUser and a.idSkill=7) 'seven' from skill b) t where t.four=1 and t.nine=1 and t.seven=1 select * from (select distinct b.idUser, (select 1 from Skill a where a.idUser=b.idUser and a.idSkill=4) 'four', (select 1 from Skill a where a.idUser=b.idUser and a.idSkill=9) 'nine', (从技能 a 中选择 1,其中 a.idUser=b.idUser 和 a.idSkill=7) 来自技能 b) t 中的 'seven',其中 t.four=1 和 t.nine= 1 和 t.seven=1

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

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