简体   繁体   中英

How to insert only one column data into a table from another table in SQL

I've two table languages and category .

  Languages                Category
+-----------+           +---------+------+
|  Language |           |   Name  | Year |
+-----------+           +---------+------+
|  English  |           | English | 2018 |
|  German   |           | German  | 2018 |
|  French   |           | French  | 2018 |
+-----------+           +---------+------+

Now, Problem is i've query like

INSERT INTO categories ( name ) select language from languages ;

But, i want to add those years(always same year) too at the same time of Language insertion into category table which is table 2 .

You may just insert a literal value for the year:

INSERT INTO categories (name, year)
SELECT language, 2018    -- or maybe use YEAR(NOW()) in place of 2018
FROM languages;

Literals can be specified in a select clause just as any column name may be specified.

seems you need year(curdate())

INSERT INTO categories (name, year) 
select language, year(curdate())
from languages ;

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