该表的架构是:
CREATE TABLE `authors` (
`authorid` int(10) NOT NULL AUTO_INCREMENT,
`emailaddress` varchar(255) DEFAULT NULL,
'send' int(1) DEFAULT '0',
`status` int(1) DEFAULT '0',
PRIMARY KEY (`authorid`),
UNIQUE KEY `authorid` (`authorid`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
示例数据:
123 john@smith.com;sue@test.org 1
5271 sally@john.doe 0
834 jacob@tom.smith;foo@bar.net; big@data.cow 1
27 tuesday@rubys.eat 1
1977 (null) 0
224 1
88 miles@per.hour 1
注意:上面的(null)
不是文本null,而是实际的null值
我想要的是一个可以从第二列获取数据并返回结果集的查询,如下所示:
id email
------------------------
123 john@smith.com
123 sue@test.org
5271 sally@john.doe
834 jacob@tom.smith
834 foo@bar.net
834 big@data.cow
27 ruby@tuesdays.eat
88 miles@per.hour
我正在寻找更通用的东西,例如 this example for SQL Server
SELECT A.[State],
Split.a.value('.', 'VARCHAR(100)') AS String
FROM (SELECT [State],
CAST ('<M>' + REPLACE([City], ',', '</M><M>') + '</M>' AS XML) AS String
FROM TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a);
这完全我想做的事情,但我想在 MySQL 中完成(正如您可以从图片中看到上述 SQL Server 查询的演示)
我坚信,当提供一种用某种语言做某事的方法时,按理说一定有一种方法可以撤消同样的事情。在 MySQL 中,您可以使用 GROUP_CONCAT
以一种方式透视数据,因此必须有一些方法来取消数据分组。
最佳答案
你可以用这样的查询来做到这一点
SELECT * FROM (
SELECT `authorid`
, TRIM(SUBSTRING_INDEX(emailaddress, ';', 1)) AS email
FROM authors
UNION ALL
SELECT `authorid`
, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(emailaddress,';;'), ';', 2),';',-1)) AS email
FROM authors
UNION ALL
SELECT `authorid`
, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(emailaddress,';;'), ';', 3),';',-1)) AS email
FROM authors
) as emails
WHERE email is not null AND email <> '';
示例
mysql> SELECT * FROM authors; +----+----------+-------------------------------------------+
| id | authorid | emailaddress |
+----+----------+-------------------------------------------+
| 1 | 123 | john@smith.com;sue@test.org |
| 2 | 5271 | sally@john.doe |
| 3 | 834 | jacob@tom.smith;foo@bar.net; big@data.cow |
| 4 | 27 | tuesday@rubys.eat |
| 5 | 1977 | NULL |
| 6 | 224 | |
| 7 | 88 | miles@per.hour |
+----+----------+-------------------------------------------+
7 rows in set (0,00 sec)
mysql> SELECT * FROM (
-> SELECT `authorid`
-> , TRIM(SUBSTRING_INDEX(emailaddress, ';', 1)) AS email
-> FROM authors
-> UNION ALL
-> SELECT `authorid`
-> , TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(emailaddress,';;'), ';', 2),';',-1)) AS email
-> FROM authors
-> UNION ALL
-> SELECT `authorid`
-> , TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(emailaddress,';;'), ';', 3),';',-1)) AS email
-> FROM authors
-> ) as emails
-> WHERE email is not null AND email <> '';
+----------+-------------------+
| authorid | email |
+----------+-------------------+
| 123 | john@smith.com |
| 5271 | sally@john.doe |
| 834 | jacob@tom.smith |
| 27 | tuesday@rubys.eat |
| 88 | miles@per.hour |
| 123 | sue@test.org |
| 834 | foo@bar.net |
| 834 | big@data.cow |
+----------+-------------------+
8 rows in set (0,00 sec)
mysql>
关于mysql - 如果字段包含分隔符则拆分并保留相关行值 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323901/