根据 GROUP_CONCAT 返回的逗号分隔列表中的 ID 提取更多记录
Posted
技术标签:
【中文标题】根据 GROUP_CONCAT 返回的逗号分隔列表中的 ID 提取更多记录【英文标题】:Pull more records based on IDs in comma separated list returned by GROUP_CONCAT 【发布时间】:2016-01-17 16:29:32 【问题描述】:是否可以?在这里可以进行 LEFT JOIN 吗?如果是这样,怎么办。我一直无法弄清楚这一点,此外,我已经用谷歌搜索了很多。
我有这两张表:
CREATE Table users (
id BIGINT(100) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
);
CREATE Table articles (
id BIGINT(100) AUTO_INCREMENT PRIMARY KEY,
author_id BIGINT(100) NOT NULL,
title VARCHAR(50) NOT NULL,
content TEXT,
published DATETIME NOT NULL
);
插入:
INSERT INTO users (username, password)
VALUES ('Bob', '5f4dcc3b5aa765d61d8327deb882cf99');
INSERT INTO users (username, password)
VALUES ('David', '630bf032efe4507f2c57b280995925a9');
INSERT INTO articles (author_id, title, content, published)
VALUES ('1', 'Science 101', 'Science is the concerted human effort to understand, or to understand better, the history of the natural world and how the natural world works, with observable physical evidence as the basis of that understanding1. It is done through observation of natural phenomena, and/or through experimentation that tries to simulate natural processes under controlled conditions.',
'2015-11-30 09:18:43');
INSERT INTO articles (author_id, title, content, published)
VALUES ('1', 'Health Care', 'Health care or healthcare is the maintenance or improvement of health via the diagnosis, treatment, and prevention of disease, illness, injury, and other physical and mental impairments in human beings.',
'2016-01-10 15:20:43');
INSERT INTO articles (author_id, title, content, published)
VALUES ('2', 'Physics 101', 'Physics is a natural science based on experiments, measurements and mathematical analysis with the purpose of finding quantitative physical laws for everything from the nanoworld of the microcosmos to the planets, solar systems and galaxies that occupy the macrocosmos.',
'2016-01-17 14:18:43');
查询:
SELECT
articles.id,
articles.author_id,
users.username,
COUNT(articles.id) AS number_of_articles,
GROUP_CONCAT(DISTINCT articles.id) AS articles_published
FROM articles
LEFT JOIN users ON articles.author_id = users.id
GROUP BY articles.author_id
ORDER BY articles.published DESC;
结果:
id author_id username number_of_articles articles_published
3 2 David 1 3
2 1 Bob 2 2,1
预期结果:
Bob's articles
Health Care
Health care or healthcare is the maintenance or improvement of health via the diagnosis, treatment, and prevention of disease, illness, injury, and other physical and mental impairments in human beings.
Science 101
Science is the concerted human effort to understand, or to understand better, the history of the natural world and how the natural world works, with observable physical evidence as the basis of that understanding1. It is done through observation of natural phenomena, and/or through experimentation that tries to simulate natural processes under controlled conditions.
------
David's articles
Physics 101
Physics is a natural science based on experiments, measurements and mathematical analysis with the purpose of finding quantitative physical laws for everything from the nanoworld of the microcosmos to the planets, solar systems and galaxies that occupy the macrocosmos.
-----
SQLfiddle:http://sqlfiddle.com/#!9/964ed/1
【问题讨论】:
请编辑您的问题并显示您希望看到的结果。 通常情况下,数据显示问题最好在表示层/应用程序级代码(例如 php)中处理,前提是该代码可用。 如果你用 PHP 循环结果,那么忘记 GROUP_CONCAT。 因为粗略的估计,在 SQL 中以 GROUP_CONCAT 为解决方案是没有问题的。在这种情况下,您正在获取一个规范化的数据集,并将其压缩为一个格式错误的数组,然后 PHP 需要解开该数组。只需将数据集吐出为格式良好的数组,并在 PHP 中解析该数组。 PHP 真的不是我的强项……但我可以把一些想法放在一起…… 【参考方案1】:这是一个粗略的例子......
<?php
include('path/to/connection/stateme.nts');
$query = "
SELECT DISTINCT a.id
, a.author_id
, u.username
FROM articles a
LEFT
JOIN users u
ON u.id = a.author_id
ORDER
BY u.username
, a.published DESC;
";
$array = array();
$result = mysqli_query($db,$query);
while($row = mysqli_fetch_assoc($result))
$array[] = $row;
// end of while loop
foreach($array as $key)
$new_array[$key['username']][] = $key['id'];
print_r($new_array);
?>
这会吐出一个这样的数组...
Array
(
[Bob] => Array
(
[0] => 2
[1] => 1
)
[David] => Array
(
[0] => 3
)
)
...
其中用户名是子数组的键,并且文章数等于该子数组中的项目数(如果我的术语有误,请道歉)
【讨论】:
这是我在发布之前所做的事情!用 PHP 操作数组。 所以我猜最终输出的表示是在应用程序级别操作的,没有其他方法可以像从 mysql 那样获取它。因此,它确认了我最初在此处发布之前所做的事情是正确的并且方向正确。谢谢@Strawberry。 @JohnSmith 并不是说“没有其他方法”,而是考虑到您无论如何都在用 PHP 操作输出,那么您的方法有什么好处?我在某种程度上同意,如果您正在做一些更复杂的数据聚合,那么可能会有使用 GROUP_CONCAT 的论点,但仅此而已。一般来说,现在的趋势似乎是构造数组,然后将它们作为 json 吐出,以便用 javascript 进行操作。 @JohnSmith 因为他没有你聪明。他正在解析嵌套在数组中的字符串。您正在解析嵌套在数组中的数组。这更聪明,对吧?因为您可以使用相同的逻辑来解析多维数组的每个维度。 我已经回答过了。你有权不同意我的回答。以上是关于根据 GROUP_CONCAT 返回的逗号分隔列表中的 ID 提取更多记录的主要内容,如果未能解决你的问题,请参考以下文章
PHP 和 MySQL GROUP_CONCAT 带分隔符逗号并搜索逗号连接的位置