简体   繁体   中英

Get multiple SQL rows with similar column value

I'm making a cron job where it publishes (inserting new into the database) an article. 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:

+----------+-------------+
| 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? I've tried using this query below but it only returns one result, which is the tigers filename.

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:

$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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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