简体   繁体   English

多行输出到MySQL中的变量

[英]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 . 以下查询似乎仅在有一个dateOfBirth设置为1975-02-28单个条目dateOfBirth 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? 是否可以删除属性dateOfBirth设置为1975-02-28而不使用子查询的用户的所有地址?

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. 我得到的确切错误是: Error Code: 1242 Subquery returns more than 1 row. .

The error means that your inner query is not returning a single row when you try to assign the id to a variable. 该错误意味着当您尝试将id分配给变量时,内部查询不会返回单行。

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? 是否可以删除属性dateOfBirth设置为1975-02-28而不使用子查询的用户的所有地址?

As told by Tonio, change your DELETE query to his suggestion and it's gonna work. 正如托尼奥所说,将你的DELETE查询更改为他的建议,它会起作用。

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

If there are more than one such entry, the SELECT subquery will return a table of IDs. 如果有多个此类条目, SELECT子查询将返回一个ID表。

You cannot have an ID be equal to a table. 您不能让ID等于表。 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: 看起来,根据以下查询尝试,在MySQL变量中存储多个值是根本不可能的:

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 ). 可以在SO上找到这些答案的几个示例(例如: 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. 在任何情况下,正如您在我的答案的第一部分所看到的那样,在这种特殊情况下,您实际上并不需要通过变量。

The problem is that subquery is returning more than one ID, if you only want 1 ID you should limit the results stating limit 1 at the end of the subquery. 问题是子查询返回多个ID,如果您只需要1个ID,则应限制在子查询末尾指定limit 1的结果。

On the other hand, a better solution to the problem is provided by joins : 另一方面,连接提供了更好的解决方案:

delete a from address a join user u on (a.user_id=u.id) 
where u.dateOfBirth='1975-02-28';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM