简体   繁体   中英

update mysql select into insert statement

I have the following tables:

documents

| id  | title             | authors |
-------------------------------------
| 1   | Arms and the Man  |         |
| 2   | East of Eden      |         |
| 3   | If Not Now, When? |         |

authors

| id  | initial | lastname     |
--------------------------------
| 1   | J       | Bloggs       |
| 2   | J       | Doe          |
| 3   | P       | Punchclock   |
| 4   | D       | Botts        |

authorships

| id  | document_id  | author_id |
----------------------------------
| 1   | 1            | 1         |
| 2   | 1            | 2         |
| 3   | 1            | 3         |
| 4   | 2            | 3         |
| 5   | 2            | 4         |
| 6   | 3            | 1         |
| 7   | 3            | 3         |
| 8   | 3            | 4         |

and I have the following sql statement:

select d.id, d.title, 
group_concat(concat(a.lastname,', ', a.initial, '.') 
             order by a.lastname, a.initial separator ' ') authors
from documents d
inner join authorships ash on ash.document_id = d.id
inner join authors a on ash.author_id = a.id
group by d.id, d.title

that returns the following result:

| id  | title             | authors                             |
-----------------------------------------------------------------
| 1   | Arms and the Man  | Bloggs, J. Doe, J. Punchclock, P.   |
| 2   | East of Eden      | Punchclock, P. Botts, D.            |
| 3   | If Not Now, When? | Bloggs, J. Punchclock, P. Botts, D. |

I need to convert my select into an update statement that updates the authors column in the documents table with the results shown in the SQL statement.

I am guessing I need to embed the select statement within an update statement somehow ? this is what I tried although incorrect:

update p set authors = group_concat(concat(a.lastname,', ', a.initial, '.') 
             order by a.lastname, a.initial separator ' ')
from documents d
inner join authorships ash on ash.document_id = d.id
inner join authors a on ash.author_id = a.id
group by d.id, d.title

I would really advise you to create a View with this "computed data", instead of trying to put this denormalized value in your table, if you really want to have these values in db. If you don't, you'll have to create triggers to keep these values "up-to-date", and you'll overcomplicate your life.

Now, for "theoretical solution"

UPDATE documents base_d
inner join
  (select d.id, d.title, 
   group_concat(concat(a.lastname,', ', a.initial, '.') 
             order by a.lastname, a.initial separator ' ') authors
   from documents d
   inner join authorships ash on ash.document_id = d.id
   inner join authors a on ash.author_id = a.id
   group by d.id, d.title) as d1
 on base_d.id = d1.id
 set base_d.authors = d1.authors;

the view solution :

create view v_documents_withAuthors as
(select d.id, d.title, 
group_concat(concat(a.lastname,', ', a.initial, '.') 
             order by a.lastname, a.initial separator ' ') authors
from documents d
inner join authorships ash on ash.document_id = d.id
inner join authors a on ash.author_id = a.id
group by d.id, d.title)

see SqlFiddle

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