Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error when using joins in query

Tags:

sql

mysql

I have this query:

SELECT * FROM `employee_activities` a
LEFT JOIN `activity` b ON a.activity_code = b.code
LEFT JOIN `employees` c ON a.employee_code = c.code
WHERE b.type = "Design"
AND c.code NOT IN(
    SELECT * FROM `employee_activities` a
        LEFT JOIN `activity` b ON a.activity_code = b.code
        LEFT JOIN `employees` c ON a.employee_code = c.code
        WHERE b.type = "Testing"
)
GROUP BY c.code

I get this error:

#1241 - Operand should contain 1 column(s)

I'm tying to get all employees that have at least one activity of type "Design" and None activity of type "Testing".

I have a query that works but I would like it to work with joins.

This works:

SELECT c.name FROM `employee_activities` a, `activity` b, `employees` c
WHERE a.activity_code = b.code
AND a.employee_code = c.code
AND b.type = "Design"

AND c.code NOT IN(
    SELECT c.code FROM `employee_activities` a, `activity` b, `employees` c
        WHERE a.activity_code = b.code
        AND a.employee_code = c.code
        AND b.type = "Testing"
)
GROUP BY c.code

What did I do wrong on the sql with joins?

like image 517
Paul Moldovan Avatar asked Jun 28 '13 12:06

Paul Moldovan


People also ask

Does join affect query performance?

Even though the join order has no impact on the final result, it still affects performance. The optimizer will therefore evaluate all possible join order permutations and select the best one. That means that just optimizing a complex statement might become a performance problem.

What does (+) mean in SQL joins?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.

Should I avoid joins in SQL?

Tl;dr: Avoid joins on large tables and evaluate parts of queries beforehand to get 100–10,000x performance gains! As mentioned in a previous post, because of some of our tables growing in size, our queries started performing poorly which resulted in a performance hit to our most used APIs.

Does join works on NULL values?

As we have seen from the above examples joining NULL values does not work. Even though you have two NULL values SQL Server does not treat these as the same value. Internally a value of NULL is an unknown value and therefore SQL Server does not equate an unknown value being equal to another unknown value.

What are the conditions for joins in SQL?

A join condition involves columns that relate two tables in some logical way. A join condition may involve more than one column. This is usually the case when a foreign key constraint consists of multiple columns. The total number of join conditions in a query is always equal to the total number of tables less one.


1 Answers

For the not in sub query - it should contain only one column - for example

SELECT * FROM `employee_activities` a
LEFT JOIN `activity` b ON a.activity_code = b.code
LEFT JOIN `employees` c ON a.employee_code = c.code
WHERE b.type = "Design"
AND c.code NOT IN(
    SELECT b.employee_code FROM `employee_activities` a
        LEFT JOIN `activity` b ON a.activity_code = b.code
        LEFT JOIN `employees` c ON a.employee_code = c.code
        WHERE b.type = "Testing"
)
GROUP BY c.code
like image 188
Ian Kenney Avatar answered Oct 03 '22 23:10

Ian Kenney