0

I'm writing a SQL script to check the integrity of a MySQL DB. The queries check different aspects of different tables, so they're not related. I don't want to relate them. I just want to see the results of the different queries one after the other.

The individual queries work fine, but I'd like to run them at the same time, but keep the results separate. They can return nothing or they can return multiple rows. (Ideally, I'd like headings to make the results more legible.)

The error is

"Error code: 1242. Subquery returns more than 1 row"

Here's what I've tried:

SELECT
(-- Teams not associated with any employees
SELECT id 
  FROM teams
 WHERE NOT EXISTS
          (SELECT id 
             FROM employee_teams
            WHERE employee_teams.teamID IN(teams.id))) AS TeamNoEmployee,

(-- Teams deleted but associated with employees
SELECT teamID 
  FROM employee_teams
 WHERE NOT EXISTS
          (SELECT id 
             FROM teams
            WHERE teams.id IN(employee_teams.teamID))) AS TeamsDeleted

Update: @JohnHC got it. Here's my new version. I need to be able to add a header for each query so I can tell them apart.

-- Teams not associated with any employees
SELECT 'Team No Employee' AS Test, id
  FROM teams
 WHERE NOT EXISTS
          (SELECT id 
             FROM employee_teams
            WHERE employee_teams.teamID IN(teams.id))
UNION
-- Teams deleted but associated with employees
SELECT 'Teams Deleted', teamID 
  FROM employee_teams
 WHERE NOT EXISTS
          (SELECT id 
             FROM teams
            WHERE teams.id IN(employee_teams.teamID))
3
  • 3
    what about using UNION ALL? Commented Aug 25, 2017 at 8:06
  • the Problem is that you may have a different number of Teams in each subquery, so a different number of rows. and SQL does not like that. Commented Aug 25, 2017 at 8:11
  • Thx @RealCheeseLord. How do I just show the results of each sub query separately? Commented Aug 25, 2017 at 14:03

1 Answer 1

3

You're not really relating those in a table. Try it the other way round, like this:

select 'Empty Team' as Scenario, id
from teams t1
left join employee_teams et
  on et.teamID = t1.id
where et.id is null
union 
select 'Deleted Team', teamid
from employee_teams et
left join teams t2
  on t2.id = et.teamID
where t2.id is null
Sign up to request clarification or add additional context in comments.

11 Comments

Check the second query. OP needs team ID, I would expect something like SELECT DISTINCT teamID ..
@Serg good spot, thanks. No distinct, I've moved to a union instead of union all
Thanks for the answer. I actually don't want want to relate them. I just want to see the results of the different queries one after the other.
@beachCode What do you mean by "I don't want to relate them"?
Sorry if I wasn't clear. I just want to see the results of the different queries. I don't need them combined into a single results set
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.