skip to Main Content

So basically I need to get people where their skill = ‘Python’ and skill=’Javascript’ however I am getting an empty set when I run this query:

SELECT uid 
FROM personal_hard_skills 
WHERE 
(skill='Python' AND level >) 
AND skill='Javascript';

I searched the internet and people were suggesting using a form of an intersection query. Would that work with different from the same table?

So for context if I run the query to get everything in this table:

mysql> SELECT uid FROM personal_hard_skills;
+----+-------+-----------------+------------+-------+---------------------+
| id | uid   | skill           | skill_code | level | timestamp           |
+----+-------+-----------------+------------+-------+---------------------+
| 25 | Dave  | Javascript      |          1 |  3.00 | 2019-07-18 19:50:19 |
| 26 | Dave  | Python          |          3 |  5.00 | 2019-07-18 19:50:19 |
| 27 | John  | Javascript      |          1 |  1.00 | 2019-07-18 19:51:23 |
| 28 | John  | Python          |          3 |  3.45 | 2019-07-18 19:51:23 |
| 29 | John  | Adobe Photoshop |          8 |  4.20 | 2019-07-18 19:51:23 |
| 30 | John  | HTML            |         64 |  2.50 | 2019-07-18 19:51:24 |
| 31 | John  | CSS             |         49 |  0.82 | 2019-07-18 19:51:24 |
| 35 | Helen | Javascript      |          1 |  4.35 | 2019-07-18 19:51:52 |
| 36 | Helen | Python          |          3 |  4.99 | 2019-07-18 19:51:52 |
| 37 | Helen | CSS             |         49 |  3.50 | 2019-07-18 19:51:52 |
+----+-------+-----------------+------------+-------+---------------------+
10 rows in set (0.03 sec)

But when I run this query:

mysql> SELECT uid FROM personal_hard_skills WHERE skill='Python' AND skill='Javascript';
Empty set (0.03 sec)

2

Answers


  1. You could use aggregation:

    SELECT uid 
    FROM personal_hard_skills 
    WHERE skill IN ('Python','Javascript')
    GROUP BY uid
    HAVING COUNT(DISTINCT skill) = 2;
    

    EDIT:

    SELECT uid
    FROM personal_hard_skills 
    WHERE (skill = 'Python' AND level >= 4.0) OR (skill = 'JavaScript' AND level >= 2.0)
    GROUP BY uid
    HAVING COUNT(DISTINCT skill) = 2;
    
    Login or Signup to reply.
  2. Combining feedback from the original post and comments, this should work:

    SELECT uid
      FROM personal_hard_skills
      WHERE (skill = 'Python' AND level >= 4.0)
      OR (skill = 'JavaScript' AND level >= 2.0);
    

    The issue in the original post was AND— no personal_hard_skills can have a skill of ‘Python’ and ‘JavaScript’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search