根据GROUP_CONCAT返回的逗号分隔列表中的ID拉出更多记录

时间:2016-01-17 16:29:32

标签: mysql sql

是否可以根据GROUP_CONCAT以逗号分隔列表返回的ID来提取更多记录? 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

1 个答案:

答案 0 :(得分: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
        )

)

...

其中username是子数组的关键字,文章数等于该子数组中的项数(如果我的术语错误,则道歉)

相关问题