Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple rows output into variables in MySQL

The following query seems to only work when there is one single entry with dateOfBirth set to 1975-02-28.

It fails when there are multiple records matching this condition.

Is it possible to delete all the addresses of users whose attribute dateOfBirth is set to 1975-02-28 without using a subquery?

SELECT @id:=(SELECT id
             FROM USER
             WHERE dateOfBirth='1975-02-28');
DELETE FROM Address
WHERE user_id=@id;

The exact error that I get is: Error Code: 1242 Subquery returns more than 1 row..

like image 884
Raju Avatar asked Jul 02 '14 13:07

Raju


People also ask

How do I assign multiple values to a variable in MySQL?

The following works as expected when there is a single value stored in a variable. SET @a := "20100630"; SELECT * FROM wordbase WHERE verified = @a; But it does not work when there are multiple values stored in a variable. SET @a := "'20100630', '20100701' "; SELECT * FROM wordbase WHERE verified in (@a);

How do I combine multiple rows into one in MySQL?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

How do I store SQL results in a variable?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.


2 Answers

If there are more than one such entry, the SELECT subquery will return a table of IDs.

You cannot have an ID be equal to a table. Try this:

DELETE FROM Address
WHERE user_id IN (SELECT id
                  FROM USER
                  WHERE dateOfBirth='1975-02-28');

EDIT:

It seems, it is simply not possible to store multiple values inside a MySQL variable as is attempted in the following query:

SELECT @ids:=(SELECT id
              FROM USER
              WHERE dateOfBirth='1975-02-28');

This explanatation is mainly based on many people writing this (often qualified by the mention 'as far as I know').

I could find many such answers and no page that would go against that claim. Several examples of these answers can be found on SO (for instance: https://stackoverflow.com/a/3156565/3401018).

I tend to believe that it may very well be true. In any case, as you could see with the first part of my answer, you don't actually need to go through a variable in this particular case.

like image 181
Tonio Avatar answered Sep 23 '22 21:09

Tonio


The error means that your inner query is not returning a single row when you try to assign the id to a variable.

SELECT id
FROM USER
WHERE dateOfBirth='1975-02-28'

Assigning values to variable thru queries should always be single row results. If you want to store multi column results, you could do the following:

SELECT field_1, field_2, field_3 INTO @var_a, @var_b, @var_c FROM any_table

But since you asked this:

Is it possible to delete all the addresses of users whose attribute dateOfBirth is set to 1975-02-28 without using a subquery?

As told by Tonio, change your DELETE query to his suggestion and it's gonna work.

DELETE FROM Address WHERE user_id IN (SELECT id FROM USER WHERE dateOfBirth='1975-02-28');

like image 30
Helio Gabrenha Jr Avatar answered Sep 25 '22 21:09

Helio Gabrenha Jr