SQL:如何在具有特定id的特定列中插入数据

时间:2016-04-27 10:21:59

标签: mysql sql sql-server codeigniter-3

是否可以在单个表上执行该查询?

我已经读过它可以由多个表完成(通过从第二个表中选择特定值来插入第一个表)

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';

1 个答案:

答案 0 :(得分:1)

  

是否可以在单个表上执行该查询?

是的,有可能:

CREATE TABLE Customers(ID INT IDENTITY(1,1),
                       CustomerName VARCHAR(100), Country VARCHAR(100));

INSERT INTO Customers (CustomerName, Country)
VALUES ('John', 'USA'), ('Martin','Germany');

INSERT INTO Customers (CustomerName, Country)
SELECT CustomerName, Country 
FROM Customers
WHERE Country='Germany';

SELECT *
FROM Customers;

LiveDemo

请注意,Table Spool

需要{{1}}

Halloween Effect