INSERT INTO SELECT

INSERT INTO SELECT bayonoti bir jadvaldan ma'lumotlarni olib, boshqa jadvalga kiritish uchun ishlatiladi.

SU

SQL INSERT INTO SELECT Bayonoti

INSERT INTO SELECT bayonoti bir jadvaldan ma'lumotlarni olib, boshqa jadvalga kiritish uchun ishlatiladi.

INSERT INTO SELECT bayonoti manba va maqsad jadvaldagi ma'lumot turlari mos kelishini talab qiladi.

Eslatma: Maqsad jadvalidagi mavjud yozuvlar ta'sir qilinmaydi.

INSERT INTO SELECT Sintaksisi

Bir jadvaldan boshqa jadvalga barcha ustunlarni nusxalash:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Faqat ba'zi ustunlarni bir jadvaldan boshqa jadvalga nusxalash:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Demo Ma'lumotlar Bazasi

Ushbu darslikda mashhur Northwind namunali ma'lumotlar bazasidan foydalanamiz.

Quyida "Customers" jadvalidan tanlov:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico

Va "Suppliers" jadvalidan tanlov:

SupplierIDSupplierNameContactNameAddressCityPostal CodeCountry
1Exotic LiquidCharlotte Cooper49 Gilbert St.LondonaEC1 4SDUK
2New Orleans Cajun DelightsShelley BurkeP.O. Box 78934New Orleans70117USA
3Grandma Kelly's HomesteadRegina Murphy707 Oxford Rd.Ann Arbor48104USA

SQL INSERT INTO SELECT Misollari

Quyidagi misolda "Suppliers" jadvalini "Customers" jadvaliga nusxalaydi (ma'lumotlar to'ldirilmagan ustunlar NULL qiymatini oladi):

INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;

Quyidagi misolda "Suppliers" jadvalini "Customers" jadvaliga nusxalaydi (barcha ustunlarni to'ldiradi):

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

Quyidagi misolda faqat Germaniya yetkazib beruvchilarini "Customers" jadvaliga nusxalaydi:

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

Ushbu sahifada

Xato haqida xabar berish