简体   繁体   English

获取具有相似列值的多个 SQL 行

[英]Get multiple SQL rows with similar column value

I'm making a cron job where it publishes (inserting new into the database) an article.我正在做一个 cron 工作,它发布(将新的插入数据库)一篇文章。 I was able to pull it through but there is one query I can't get to work.我能够完成它,但有一个查询我无法开始工作。 I'd like to print certain rows from another table that can be inserted to the article being published.我想打印另一个表中的某些行,这些行可以插入到正在发布的文章中。 Supposed I have this another table like this:假设我有another table

+----------+-------------+
| filename | released    |
+----------+-------------+
| tigers   | 2020-05-27  |
| wolves   | 2020-05-27  |
| earth    | 2020-05-27  |
| bamboo   | 2020-05-27  |
| glaciers | 2020-05-02  |
+----------+-------------+

How can I print the result of the filenames as:如何将文件名的结果打印为:

bamboo, earth, tigers, wolves

so that the cron can insert it to the article table 's specified column with the same format?以便cron可以将其插入到article table的指定列中以相同的格式? I've tried using this query below but it only returns one result, which is the tigers filename.我在下面尝试过使用这个查询,但它只返回一个结果,即tigers文件名。

SELECT filename,
    GROUP_CONCAT(filename ORDER BY filename ASC SEPARATOR ', ')
    FROM table
    WHERE released='2020-05-27'
    GROUP BY released

Many thanks for the help in advance!非常感谢您提前提供的帮助!

Using TheImpaler 's query, I managed to solve what I'm trying to get with the following code:使用TheImpaler的查询,我设法用以下代码解决了我想要得到的问题:

$get = $database->query("SELECT released, 
    GROUP_CONCAT(filename ORDER BY filename ASC SEPARATOR ', ')
    FROM another
    WHERE released='2020-05-27'
    GROUP BY released");

$row = mysqli_fetch_array($get);
echo $row['1']; // Prints the comma-separated result of the filenames
echo print_r($row); // Prints the entire row

Many thanks for the comments, it gave me an idea and had my query validated!非常感谢您的评论,它给了我一个想法并验证了我的查询!

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

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