简体   繁体   English

SQL-插入选择(多列)

[英]SQL - Insert Into Select (multiple columns)

front end web guy thrown into the SQL abyss. 前端网络专家陷入SQL深渊。 Below is a prototype of our current SQL query: 以下是我们当前的SQL查询的原型:

 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 我们最近添加了一个web.val5列,我们想将其信息插入到与web.val4相同的列中

I tried the following using the ALL clause: 我使用ALL子句尝试了以下操作:

 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?). 但是我一直得到0(或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? 是否可以在LEFT JOIN中添加某个子句,以使一个值等于同一张表中不同列中的两个不同值? 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. 在这里有点猜测,但是如果我理解您的问题,则您and条件会使其返回null值。 Instead you want or or in : 相反,您想要orin

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM