SQL:从另一个表中选择字段

时间:2018-04-01 19:50:07

标签: mysql sql

我是sql的新手,我感谢以下方面的帮助: 我有2个表:table1包含userID和字段y1,而表2包含userID和三个附加字段:x1,x2,x3和x4 对于table1中的每一行,如果x1,x2和x3不为null,我需要从表2中选择x4。 UserID可以在table1或table2中出现多次,重要的是我需要结果列行与table1中一样。 感谢

以下是一个例子:

表1:

P.when('A').register("ImageBlockATF", function(A){
var data = {
            'colorImages': { 'initial': [{"hiRes":null,"thumb":"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL._SX355_.jpg":[347,355],"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL._SX450_.jpg":[440,450],"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL._SX425_.jpg":[416,425],"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL._SX466_.jpg":[456,466],"https://images-na.ssl-images-amazon.com/images/I/41947FUviFL.jpg":[489,500]},"variant":"MAIN","lowRes":null},{"hiRes":null,"thumb":"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL._SX355_.jpg":[302,355],"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL._SX450_.jpg":[383,450],"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL._SX425_.jpg":[361,425],"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL._SX466_.jpg":[396,466],"https://images-na.ssl-images-amazon.com/images/I/31SqBz2H7EL.jpg":[425,500]},"variant":"PT01","lowRes":null},{"hiRes":null,"thumb":"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL._SX355_.jpg":[192,355],"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL._SX450_.jpg":[244,450],"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL._SX425_.jpg":[230,425],"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL._SX466_.jpg":[253,466],"https://images-na.ssl-images-amazon.com/images/I/31Cw2c6RFsL.jpg":[271,500]},"variant":"PT02","lowRes":null},{"hiRes":null,"thumb":"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L._SX355_.jpg":[222,355],"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L._SX450_.jpg":[281,450],"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L._SX425_.jpg":[265,425],"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L._SX466_.jpg":[291,466],"https://images-na.ssl-images-amazon.com/images/I/51wYWSUvN8L.jpg":[312,500]},"variant":"PT03","lowRes":null},{"hiRes":null,"thumb":"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL._SX355_.jpg":[222,355],"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL._SX450_.jpg":[281,450],"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL._SX425_.jpg":[265,425],"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL._SX466_.jpg":[291,466],"https://images-na.ssl-images-amazon.com/images/I/31BY-dyDpWL.jpg":[312,500]},"variant":"PT04","lowRes":null}]},
            'colorToAsin': {'initial': {}},
            'holderRatio': 1.0,
            'holderMaxHeight': 700,
            'heroImage': {'initial': []},
            'heroVideo': {'initial': []},
            'spin360ColorData': {'initial': {}},
            'spin360ColorEnabled': {'initial': 0},
            'spin360ConfigEnabled': false,
            'playVideoInImmersiveView':'false',
            'tabbedImmersiveViewTreatment':'T2',
            'totalVideoCount':'0',
            'videoIngressATFSlateThumbURL':'',
            'mediaTypeCount':'0',
            'atfEnhancedHoverOverlay' : true,
            'weblabs' : {},
            'aibExp3Layout' : 1,
            'aibRuleName' : 'R18'
            };
A.trigger('P.AboveTheFold'); // trigger ATF event.
return data;
});

表2:

UserID y1
1            long
2            short

x1 =蓝色,x2 =圆,x3 =大

的结果
UserID x1 x2 x3 x4
1            blue round big 11
1            blue square big 22
2            blue round   big 33
3            red square  small 77

2 个答案:

答案 0 :(得分:0)

不完全是你最后一行的意思,但我假设你想要table1中的所有行添加table2.x4。

您的查询将是这样的: SELECT table1。*,table2.x4 from table1 LEFT OUTER JOIN table2 ON table1.UserID = table2.UserID AND NOT(table2.x1 IS NULL)AND NOT(table2.x2 IS NULL)NOT(table2.x3 IS NULL)

答案 1 :(得分:0)

你似乎想要:

select t1.*, t2.x4
from table1 t1 join
     table2 t2
     on t1.userid = t2.userid
where t2.x1 = 'blue' and t2.x2 = 'round' and t2.x3 = 'big';