简体   繁体   中英

Insert in a table based on result of a select from another table

I have 2 Tables Named Shop and Location

  • table Shop has a column name

  • table Location has a column description

For every line of table shop, I want to insert a line in table Location that respect the following structure:

Location.description = Shop.name + "-Location"

So for every Shop, I want to insert a location with

description=Shop.name.concat("-Location")

Please how can I write one sql query to solve this problem.

You use insert . . . select insert . . . select insert . . . select . Presumably, you want something like this:

INSERT INTO location(description)
    SELECT DISTINCT CONCAT(s.name, '-Location')
    FORM shops s;

You can also use || for string concatenation in many databases:

INSERT INTO location(description)
    SELECT DISTINCT s.name || '-Location'
    FORM shops s;

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