I have the following MySQL table called skills.
id | idUser | idSkill |
---|---|---|
1 | 4 | 1 |
2 | 8 | 4 |
3 | 8 | 9 |
4 | 13 | 9 |
5 | 18 | 2 |
6 | 22 | 1 |
7 | 27 | 2 |
8 | 32 | 4 |
9 | 11 | 2 |
10 | 32 | 9 |
One simple approach uses aggregation:
SELECT idUser
FROM skills
WHERE idSkill IN (4, 9)
GROUP BY idUser
HAVING MIN(idSkill) <> MAX(idSkill);
The above query is sargable, meaning that an appropriate index can use theidSkill
column. Consider adding this index for added performance:
CREATE INDEX idx ON skills (idUser, idSkill);
Edit:
Use this query for 3 items:
SELECT idUser
FROM skills
WHERE idSkill IN (2, 4, 9)
GROUP BY idUser
HAVING COUNT(DISTINCT idSkill) = 3;
you can try this query. simply select userIds and add GROUP BY to avoid repeated userIds, and if you're using more skillIds like for example (4,9,2) then change the HAVING COUNT to 3 and so on.
SELECT idUser FROM your_table
WHERE idSkill IN (4, 9)
GROUP BY idUser
HAVING COUNT(DISTINCT idSkill) = 2
One more solution:
SELECT DISTINCT idUser
FROM tablename t0
WHERE EXISTS ( SELECT NULL
FROM tablename t1
WHERE t0.idUser = t1.idUser
AND t1.idSkill = 4 )
AND EXISTS ( SELECT NULL
FROM tablename t2
WHERE t0.idUser = t2.idUser
AND t2.idSkill = 9 )
The solution can be expanded by adding moreAND EXISTS ()
blocks ifidSkill
values list contains more than 2 values.
And this query is sargable. fiddle
You can check with the belowMYSQL
Query :
SELECT DISTINCT(idUser) FROM skill WHERE idSkill IN(4,9)
Try this:
SELECT DISTINCT(idUser) FROM skills
WHERE idSkill = 4 and idUser IN(select idUser from skills where idSkill = 9)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.