Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WHERE EXISTS ( SELECT * ... ) returns operand should have 1 columns, but has n #3772

Closed
druvv opened this issue Jul 7, 2022 · 2 comments
Closed
Assignees
Labels
bug Something isn't working sql Issue with SQL

Comments

@druvv
Copy link
Contributor

druvv commented Jul 7, 2022

CREATE table t1 (pk int PRIMARY KEY);
CREATE table t2 (pk int PRIMARY KEY, col1 int);
INSERT into t1 VALUES (1), (2), (3);
INSERT into t2 VALUES (1, 100), (2, 200), (3, 300);

SELECT * from t1 WHERE EXISTS (SELECT * from t2 where t1.pk = t2.pk);
/*
returns error:
operand should have 1 columns, but has 2

we expect to see results per:
https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html
*/


SELECT * from t1 WHERE EXISTS (SELECT count(*) from t2 where t1.pk = t2.pk);
/*
returns correct results:
+----+
| pk |
+----+
| 1  |
| 2  |
| 3  |
+----+
*/

SELECT * from t1 WHERE EXISTS (SELECT count(*) from t2 where t1.pk = t2.pk AND t2.col1 = 200);
/*
returns 3 rows correctly since 1 row is always returned by subquery:
+----+
| pk |
+----+
| 1  |
| 2  |
| 3  |
+----+
*/

SELECT * from t1 WHERE EXISTS (SELECT pk from t2 where t1.pk = t2.pk AND t2.col1 = 200);
/* 
EDIT: this is correct
only returns 1 row which is incorrect: 
+----+
| pk |
+----+
| 2  |
+----+
*/
@druvv druvv added bug Something isn't working sql Issue with SQL labels Jul 7, 2022
@max-hoffman
Copy link
Contributor

The last 3 are all correct afaik, the first query with the error is definitely a bug.

@jcor11599 this could be a good one for you.

@druvv druvv changed the title WHERE EXISTS ( SELECT * ... ) returns operand should have 1 columns, but has n. It also returns the incorrect number of rows sometimes. WHERE EXISTS ( SELECT * ... ) returns operand should have 1 columns, but has n Jul 7, 2022
@druvv
Copy link
Contributor Author

druvv commented Jul 7, 2022

oops, yes the last query is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sql Issue with SQL
Projects
None yet
Development

No branches or pull requests

3 participants