简体   繁体   中英

SQL - Insert Into Select (multiple columns)

front end web guy thrown into the SQL abyss. Below is a prototype of our current SQL query:

 INSERT INTO table (table column)
 SELECT ad.val1, web.val2, ad.val3
 FROM ad
 LEFT JOIN web
 ON ad.val 4 = web.val4   

We've recently added a web.val5 column and we want to insert it's information to the same column as web.val4

I tried the following using the ALL clause:

 INSERT INTO table (table column)
 SELECT ad.val1, web.val2, ad.val3
 FROM ad
 LEFT JOIN web
 ON ad.val 4 = web.val4   
 AND ad.val4 = web.val5  

But I keep getting 0 (or NULL?). Is there a certain clause that I can add to a LEFT JOIN that can equate a value to two different values in different columns within the same table? Sorry if this is confusing, I barely understand it myself.

You can consider joining it twice and match that condition like

 INSERT INTO table (table column)
 SELECT ad.val1, web.val2, ad.val3
 FROM ad
 LEFT JOIN web
 ON ad.val4 = web.val4  
 LEFT JOIN web w           //second join
 ON ad.val4 = w.val5  

Guessing a little bit here, but if I understand your issue, your and criteria is making it return the null values. Instead you want or or in :

INSERT INTO table (table column)
SELECT ad.val1, web.val2, ad.val3
FROM ad
    LEFT JOIN web ON ad.val4 IN (web.val4, web.val5)

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