I'm trying to echo info from a database onto the webpage. The page is supposed to have a bunch of article publications which includes the title, authors, journal name (if applicable), date published. For example:
I've made two tables. One contains paper_id, author_id, paper_name, journal_name, date. Second one contains author_id, author_firstname, author_lastname.
The part I'm struggling with is outputting multiple authors. in the second table I have the following for first paper
author_id: 1
author_firstname: john
author_lastname: smith
author_id: 1
author_firstname: bob
author_lastname: thomas
I want to print this: Name of the paper (with john smith, bob thomas), journal name, date published.
I've tried joining author_id but it outputs the same paper twice (since i have two authors with id 1. I need suggestions as to how i should change the table or join it differently and what not. Any help will be appreciated, thanks. I'm also a beginner programmer
不知道我是否收到您的问题,但是您有两个表,您的查询将类似于SELECT table1.id,列名FROM table1 INNER JOIN table2 ON table1.id = table2.id
I beleieve the correct strategy is to use GROUP_CONCAT
on each paper to obtain a comma separated list of authors. As a note, each group in the query below having a given paper_id
will also have the same values for paper_name
, journal_name
and date
. I include the columns other than paper_id
so that they will be available in the SELECT
clause.
SELECT t1.paper_name,
GROUP_CONCAT(CONCAT(t2.author_lastname, ', ', t2.author_firstname)
SEPARATOR ' '),
t1.journal_name, t1.date
FROM table1 t1 INNER JOIN table2 t2
ON t1.author_id = t2.author_id
GROUP BY t1.paper_id, t1.paper_name, t1.journal_name, t1.date
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.