mysqlsqlsubqueryunionmysql-error-1241

SQL SELECT DISTINCT Subquery Error: 1241


I am working on a long query that includes a SELECT DISTINCT and subquery. It is partially working, but not when put together. I am getting a Error: 1241 - Operand should contain 1 column(s) and I cannot see why. Any help would be greatly appreciated.

Here is my code:

SELECT p.projId, pc.company, s.staffId, s.fName, s.lName
    FROM projects AS p
    INNER JOIN projCorp AS pc ON p.projId = pc.projId
    INNER JOIN projStaff AS ps ON p.projId = ps.projId
    INNER JOIN staff AS s ON ps.staffId = s.StaffId
    WHERE p.projId = '9' AND s.company = pc.company
UNION
    SELECT DISTINCT p.projId, pc.company, "NA", "NA", "NA"
    FROM projects AS p
    INNER JOIN projCorp AS pc ON p.projId = pc.projId
    INNER JOIN projStaff AS ps ON p.projId = ps.projId
    WHERE p.projId = '9' AND pc.company NOT IN (SELECT p.projId, pc.company, s.staffId, s.fName, s.lName
        FROM projects AS p
        INNER JOIN projCorp AS pc ON p.projId = pc.projId
        INNER JOIN projStaff AS ps ON p.projId = ps.projId
        INNER JOIN staff AS s ON ps.staffId = s.StaffId
        WHERE p.projId = '9' AND s.company = pc.company);

Without the NOT IN subquery, I am getting this result:

result without NOT IN subquery

The reason for the NOT IN subquery is to get rid of the third duplicate entry that doesn't have a staff member associated. The result should just have the first two entries from the picture result field.


Solution

  • Snip:

    ... WHERE p.projId = '9' AND pc.company NOT IN (
            SELECT p.projId, pc.company, s.staffId, s.fName, s.lName
            FROM projects AS p ....
        )
    

    You're trying to select when company not in but there are specifying more than one selected row from within your not in query. Just select company:

    ... WHERE p.projId = '9' AND pc.company NOT IN (
            SELECT pc.company
            FROM projects AS p ....
        )