如何克服MySQL'Subquery returns more than 1 row'错误和select所有相关记录
How to overcome MySQL 'Subquery returns more than 1 row' error and select all the relevant records
Table创作
CREATE TABLE `users` (
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY(`id`)
);
CREATE TABLE `email_address` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`email_address` VARCHAR(50) NOT NULL,
INDEX pn_user_index(`user_id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
PRIMARY KEY(`id`)
);
数据插入
INSERT INTO users (id, name) VALUES (1, 'Mark'), (2, 'Tom'), (3, 'Robin');
INSERT INTO email_address (user_id, email_address) VALUES
(1, 'mark@gmail.com'), (1, 'mark@yahoo.com'), (1, 'mark@msn.com'),
(2, 'tom@gmail.com'), (2, 'tom@yahoo.com'), (2, 'tom@msn.com'),
(3, 'robin@gmail.com'), (3, 'robin@yahoo.com'), (3, 'robin@msn.com');
SQL查询
SELECT usr.name AS name
, (SELECT email.email_address
FROM email_address AS email
WHERE email.user_id = usr.id) AS email
FROM users AS usr;
使用上面的 MySQL 查询,如何避免 MySQL 错误 'Subquery returns more than 1 row ' 和 select 特定用户的所有相关电子邮件地址,如下所示。谢谢。
+----------+-------------------------------------------------+
| name | email |
+----------+-------------------------------------------------+
| Mark | mark@gmail.com, mark@yahoo.com, mark@msn.com |
| Tom | tom@gmail.com, tom@yahoo.com, tom@msn.com |
| Robin | robin@gmail.com, robin@yahoo.com, robin@msn.com |
+----------+----------+--------------------------------------+
您正在对 email_address table 进行模棱两可的 subselect 声明。换句话说:您正在尝试 select 子 select 中的多行以获得单行 selection。想想看,我会问你这个问题:"How does the database decide, with your query, which email address to select?" 答案是不能,因为你的查询没有指定要使用的电子邮件地址。因此你的错误。
您可以将您的 subselection 限制为 1 个值。这是它的样子:
SELECT usr.name AS name,
(SELECT email.email_address
FROM email_address AS email
WHERE email.user_id = usr.id
LIMIT 1)
AS email
FROM users AS usr;
不过,您最好的选择可能是将可能的电子邮件发送给 所有。为此,您需要一个 JOIN
子句,其结构可能如下所示:
SELECT usr.name AS name, addr.email AS email
FROM users AS usr
LEFT JOIN email_address AS addr ON (addr.user_id=usr.id);
这将为您提供 select 行,其中包含用户和电子邮件地址的组合。这意味着您将为具有多个电子邮件地址关联的任何用户返回多行。那就是您可以遍历所有返回的行并通过电子邮件发送所有用户的电子邮件。如果有某种方法可以识别主要电子邮件地址,那么您可能只向主要电子邮件地址发送电子邮件。
GROUP_CONCAT
与 SEPARATOR
并稍微简化您的查询:
SELECT users.name AS name,
(SELECT GROUP_CONCAT(email_address.email_address SEPARATOR ', ')
FROM email_address
WHERE email_address.user_id = users.id) AS email
FROM users
参考:https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
我宁愿这样做而不是使用子查询
select
GROUP_CONCAT(ea.email_address) as emails,
u.name
from
users u
left join email_address ea ON (u.id=ea.user_id)
group by u.id,u.name
Table创作
CREATE TABLE `users` (
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY(`id`)
);
CREATE TABLE `email_address` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`email_address` VARCHAR(50) NOT NULL,
INDEX pn_user_index(`user_id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
PRIMARY KEY(`id`)
);
数据插入
INSERT INTO users (id, name) VALUES (1, 'Mark'), (2, 'Tom'), (3, 'Robin');
INSERT INTO email_address (user_id, email_address) VALUES
(1, 'mark@gmail.com'), (1, 'mark@yahoo.com'), (1, 'mark@msn.com'),
(2, 'tom@gmail.com'), (2, 'tom@yahoo.com'), (2, 'tom@msn.com'),
(3, 'robin@gmail.com'), (3, 'robin@yahoo.com'), (3, 'robin@msn.com');
SQL查询
SELECT usr.name AS name
, (SELECT email.email_address
FROM email_address AS email
WHERE email.user_id = usr.id) AS email
FROM users AS usr;
使用上面的 MySQL 查询,如何避免 MySQL 错误 'Subquery returns more than 1 row ' 和 select 特定用户的所有相关电子邮件地址,如下所示。谢谢。
+----------+-------------------------------------------------+
| name | email |
+----------+-------------------------------------------------+
| Mark | mark@gmail.com, mark@yahoo.com, mark@msn.com |
| Tom | tom@gmail.com, tom@yahoo.com, tom@msn.com |
| Robin | robin@gmail.com, robin@yahoo.com, robin@msn.com |
+----------+----------+--------------------------------------+
您正在对 email_address table 进行模棱两可的 subselect 声明。换句话说:您正在尝试 select 子 select 中的多行以获得单行 selection。想想看,我会问你这个问题:"How does the database decide, with your query, which email address to select?" 答案是不能,因为你的查询没有指定要使用的电子邮件地址。因此你的错误。
您可以将您的 subselection 限制为 1 个值。这是它的样子:
SELECT usr.name AS name,
(SELECT email.email_address
FROM email_address AS email
WHERE email.user_id = usr.id
LIMIT 1)
AS email
FROM users AS usr;
不过,您最好的选择可能是将可能的电子邮件发送给 所有。为此,您需要一个 JOIN
子句,其结构可能如下所示:
SELECT usr.name AS name, addr.email AS email
FROM users AS usr
LEFT JOIN email_address AS addr ON (addr.user_id=usr.id);
这将为您提供 select 行,其中包含用户和电子邮件地址的组合。这意味着您将为具有多个电子邮件地址关联的任何用户返回多行。那就是您可以遍历所有返回的行并通过电子邮件发送所有用户的电子邮件。如果有某种方法可以识别主要电子邮件地址,那么您可能只向主要电子邮件地址发送电子邮件。
GROUP_CONCAT
与 SEPARATOR
并稍微简化您的查询:
SELECT users.name AS name,
(SELECT GROUP_CONCAT(email_address.email_address SEPARATOR ', ')
FROM email_address
WHERE email_address.user_id = users.id) AS email
FROM users
参考:https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
我宁愿这样做而不是使用子查询
select
GROUP_CONCAT(ea.email_address) as emails,
u.name
from
users u
left join email_address ea ON (u.id=ea.user_id)
group by u.id,u.name