开发者

Inserting rows to a table from another SELECT - MySQL

开发者 https://www.devze.com 2023-02-14 14:37 出处:网络
I have a table called CountriesList with ID and Country columns. I have another table called开发者_如何学Python Entries which have a column named Country.

I have a table called CountriesList with ID and Country columns.

I have another table called开发者_如何学Python Entries which have a column named Country.

I would like to INSERT all the countries in Entries into CountriesList... I am using the following syntax:

INSERT INTO CountriesList ('Country') 
SELECT DISTINCT ('Country')
FROM  `Entries`

However, I get

1064 - You have an error in your SQL syntax

What is wrong with the syntax?


Don't mix up single quotes (') and backticks (`).

Backticks are for database and column names.

Single quotes are used for strings.

INSERT INTO `CountriesList`
(`Country`) 
SELECT DISTINCT `Country` FROM `Entries`


Insert into entries(country) 

     select distinct(country) from 

    countrylist where country not in

     (select country from entries)


It should be -

INSERT INTO CountriesList
(Country) 
SELECT DISTINCT Country FROM  Entries


try this insert into Entries (country) select country from CountriesList

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号