两个表上的SQL连接语句

时间:2016-10-12 14:52:27

标签: sql oracle

我在两个表Product_Number和Manufacturer上创建了两个表Product_Tree和Product之间的连接,以返回匹配的列。

以下是2个表格:

表:Product_Tree

Col1,Col2,Model_Number,Col3,Manufacturer,Col4
111111,Pepsi,aaa,111111,aaa,description
222222,Miranda,bbb,222222,bbb,'description
333333,Cola,bbb,333333,bbb,description

表:产品

Model_Number,Manufacturer
a,a
b,b
c,c
d,d

以下是查询:

SELECT Product_Tree.col0,Product_Tree.col1,Product_Tree.col2,Product_Tree.col3,Product_Tree.Model_Number,Product_Tree.Manufacturer 
FROM Product_Tree 
JOIN Product ON Product.model_number LIKE ''''%''''Product_Tree.MODEL_NUMBER''''%'''' 
AND Product.manufacturer LIKE ''''%''Product_Tree.MANUFACTURER''''%'''';

我收到此错误:

ORA-00911: invalid character

1 个答案:

答案 0 :(得分:1)

您需要使用连接运算符将%通配符连接到列product_tree.manufacturer

SELECT Product_Tree.col0,Product_Tree.col1,Product_Tree.col2,Product_Tree.col3,Product_Tree.Model_Number,Product_Tree.Manufacturer 
FROM Product_Tree 
JOIN Product ON Product.model_number LIKE '%' || Product_Tree.MODEL_NUMBER || '%' 
AND Product.manufacturer LIKE '%' || Product_Tree.MANUFACTURER || '%';

我猜这个查询是在一个脚本中,并使用单引号'引用,这就是为什么你在这里有单引号的原因。如果是这种情况,那么引用的SQL语句将是:

SELECT Product_Tree.col0,Product_Tree.col1,Product_Tree.col2,Product_Tree.col3,Product_Tree.Model_Number,Product_Tree.Manufacturer 
FROM Product_Tree 
JOIN Product ON Product.model_number LIKE ''%'' || Product_Tree.MODEL_NUMBER || ''%'' 
AND Product.manufacturer LIKE ''%'' || Product_Tree.MANUFACTURER || ''%'';