2017-05-10 6 views
-1

を複製リターンに参加、私は(それは別のテーブルから来ている一つの会社に割り当てられた製品を持っており、ref_idで表さ左のテーブルを持っている、と私は持っています質問のために簡略化しました)、私はマスターリストを持っています。私はA社のすべての製品をマスターリストと比較し、A社が購読していないすべての製品を表示したいと考えています。私はJOINを使用していて、PHPプログラムをオフにする重複する列名を取得することになります。ここでは、私がRIGHT、LEFTはオペレーターに参加しないで、JOIN、UNIONのためのいくつかの構文を試してきたものであり、以下しかし、私は重複する列名を取得しています、私が得ている最も近いと私は最後の2列の最後のテーブルを必要としています。お知らせ下さい。MYSQLは、私はSQLに新しいですし、次のような課題を持っている列名

mysql> select ref_id, product_id, short_name from ap_company_product order by ref_id, product_id; 
+--------+------------+------------+ 
| ref_id | product_id | short_name | 
+--------+------------+------------+ 
|      2 | 10         | product 10 | 
|      2 | 11         | product 11 | 
|      2 | 12         | product 12 | 
|      2 | 15         | product 15 | 
|      2 | 17         | product 17 | 
|      2 | 21         | product 21 | 
|      3 | 11         | product 11 | 
|      3 | 13         | product 13 | 
|      3 | 17         | product 17 | 
|      3 | 20         | product 20 | 
+--------+------------+------------+ 
10 rows in set (0.00 sec) 

THE MAIN LIST 

mysql> select  product_id, short_name from ap_company_product_list; 
+------------+-------------+ 
| product_id | short_name  | 
+------------+-------------+ 
| 10         | product 10  | 
| 11         | product 11  | 
| 12         | product 12  | 
| 13         | product 13  | 
| 14         | product 14  | 
| 15         | product 15  | 
| 16         | product 16  | 
| 17         | product 17  | 
| 18         | product 18  | 
| 19         | product 19  | 
| 20         | product 20  | 
| 21         | product 21  | 
| 22         | product 22  | 
+------------+-------------+ 
13 rows in set (0.00 sec) 


Another SQL which is the actual results I need 

SELECT aa.product_id as product_id, aa.short_name as short_name, aa.ref_id as ref_id, bb.product_id as product_id, bb.short_name as short_name FROM (select ref_id, product_id, short_name from ap_company_product where ref_id=2) aa RIGHT OUTER JOIN ap_company_product_list bb ON aa.product_id = bb.product_id where aa.product_id is NULL; 
+------------+------------+--------+------------+------------+ 
| product_id | short_name | ref_id | product_id | short_name | 
+------------+------------+--------+------------+------------+ 
| NULL       | NULL       |   NULL | 13         | product 13 | 
| NULL       | NULL       |   NULL | 14         | product 14 | 
| NULL       | NULL       |   NULL | 16         | product 16 | 
| NULL       | NULL       |   NULL | 18         | product 18 | 
| NULL       | NULL       |   NULL | 19         | product 19 | 
| NULL       | NULL       |   NULL | 20         | product 20 | 
| NULL       | NULL       |   NULL | 22         | product 22 | 
+------------+------------+--------+------------+------------+ 
7 rows in set (0.01 sec) 
+0

がhttps://meta.stackoverflow.com/questions/333952/why-shouldを参照してください:

SELECT bb.product_id as product_id, bb.short_name as short_name FROM (select ref_id, product_id, short_name from ap_company_product where ref_id=2) aa RIGHT OUTER JOIN ap_company_product_list bb ON aa.product_id = bb.product_id where aa.product_id is NULL; 

ところで、私はこの1つを好むだろうが-i-提供-MCVE-用-思われるもの-に-ME-に被-非常に-シンプル-SQLクエリ – Strawberry

+0

をあなたには、いくつかの他のエイリアスを使用することはできません? – GurV

答えて

1

ただ、この使用:

SELECT product_id, short_name FROM ap_company_product_list WHERE product_id NOT IN (SELECT product_id FROM ap_company_product); 
+0

はそんなにあなたのソリューションがうまく働いていた、私は(ちょうど終わり決算prenticesを必要として)はるかに簡単、お好みの溶液で行きました、ありがとうございました。私は人生を複雑にして、ウサギ全体を去ったように見える。そんなにベンをありがとう。 – user2679455

+0

@ user2679455まったく問題ありません。 SQLは今でも難しくて深いようです。最後の結果セットのパラメータは、私はNOT IN PRODUCT_ID WHEREそれはap_company_product_list FROM PRODUCT_ID、SHORT_NAMEを選択するように変更される好ましいSQLが一つ欠けていたところで – Ben

+0

(ap_company_product FROMのproduct_idを選択WHERE ref_id = 2); – user2679455

関連する問題