[英]sql statement for two different columns
I am trying to make sql query between two columns. 我正在尝试在两列之间进行sql查询。
table_1 表格1
ID ProductName ProductDescription
1 Prod_1 Description_1
2 Prod_2 Description_1
3 Prod_3 Description_1
4 Prod_4 Description_1
5 Prod_5 Description_1
table_2 table_2
ID Product Partner
1 1 21
2 2 21
3 3 21
4 1 32
5 1 32
6 4 21
7 5 21
8 5 32
By using query below I get as a result only list of products that are selected in table_2. 通过使用下面的查询,我只能得到在table_2中选择的产品列表。 That is good I need to get that, but I also want to print all values from table_1 in same query for later programming I need that.
很好,我需要得到它,但是我也想在同一查询中打印来自table_1的所有值,以便以后需要进行编程。 Not sure is it possible to make one column that will print 1 if product_ID and ID match and if not print 0
如果product_ID和ID匹配,则不确定是否可以使一列打印1;否则,打印0
$query = "SELECT a.ID, a.ProductName, b.ID, b.Product, b.Partner
FROM table_1 a
LEFT JOIN table_2 b
ON a.ID = b.Product
WHERE b.Partner = 21"
I want to print values from table_1 match with table_2 what are selected in table_2. 我想打印来自table_1的值与table_2匹配的表_2中选择的值。 I am getting stuck here any advice is appreciated.
我被困在这里,任何建议都值得赞赏。
As i am right with your requirenments this untested query should work: 由于我对您的要求正确,因此未经测试的查询应该可以正常工作:
$query = "SELECT a.ID, a.ProductName, b.ID, b.Product, b.Partnerm, case when b.id is null then 0 else 1 end
FROM table_1 a
LEFT JOIN table_2 b
ON a.ID = b.ID and b.Partner = 21"
Try this 尝试这个
$query = "SELECT a.ID, a.ProductName, b.ID, b.Product, b.Partnerm case when b.id is null then 0 else 1 end
FROM table_1 a
LEFT JOIN table_2 b
ON a.ID = b.Product and b.Partner = 21"
$query = "SELECT a.ID, a.ProductName, b.ID, b.Product, b.Partner FROM table_1 a LEFT JOIN table_2 b
ON a.ID = b.Product WHERE b.Partner = 21"
Kinda like this since the connection between table 1 and table 2 is the ID and the product. Kinda之所以这样,是因为表1和表2之间的连接是ID和产品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.