Insert into one column by selecting another column in other table but how fill the second column

shervin - picture shervin - · Aug 7, 2012 · Viewed 74.7k times · Source

i have a table which has two columns i'd fill one of the columns by selecting other table column data but how can i fill the next column cause i can't use VALUE. Here's the code

INSERT INTO Numbers(number, val) SELECT LaptopID FROM Laptop WHERE Laptop.Pid = 2 

as you can see the "val" column left empty how can i fill that?

Answer

aF. picture aF. · Aug 7, 2012

Use NULL if the column allows it:

INSERT INTO Numbers(number, val)
SELECT LaptopID, NULL
FROM Laptop WHERE Laptop.Pid = 2

Or use the intended (hardcoded) value that you want.

If number:

INSERT INTO Numbers(number, val)
SELECT LaptopID, 2
FROM Laptop WHERE Laptop.Pid = 2

or if text:

INSERT INTO Numbers(number, val)
SELECT LaptopID, 'val'
FROM Laptop WHERE Laptop.Pid = 2