這是Table1
數據。使用SQL JOIN比較兩個表格
USER_ID | PRODUCT_ID | TIMESTAMPS
------------+------------------+-------------
1015826235 220003038067 *1004941621*
1015826235 300003861266 1005268799
1015826235 140002997245 1061569397
1015826235 *210002448035* 1005542471
如果比較Table1
數據與下面Table2
數據,然後在Table1
數據的最後一行PRODUCT_ID
沒有與ITEM_ID
最後一行在下面Table2
數據匹配,也同樣有TIMESTAMPS
在第一行Table1
的數據與CREATED_TIME
的數據不匹配,第一行的數據爲Table2
。
BUYER_ID | ITEM_ID | CREATED_TIME
-------------+--------------------+------------------------
1015826235 220003038067 *2001-11-03 19:40:21*
1015826235 300003861266 2001-11-08 18:19:59
1015826235 140002997245 2003-08-22 09:23:17
1015826235 *200002448035* 2001-11-11 22:21:11
所以我需要用下面的輸出Table2-無論JOINING表1後爲我好,顯示這樣的結果對於上面的例子。
BUYER_ID | ITEM_ID | CREATED_TIME | PRODUCT_ID | TIMESTAMPS
------------+-------------------+-------------------------+-------------------+-----------------
1015826235 220003038067 *2001-11-03 19:40:21* 220003038067 *1004941621*
1015826235 *200002448035* 2001-11-11 22:21:11 *210002448035* 1005542471
OR
BUYER_ID | ITEM_ID | CREATED_TIME | USER_ID | PRODUCT_ID | TIMESTAMPS
-----------+-------------------+-------------------------+------------------+----------------------+------------------
1015826235 220003038067 *2001-11-03 19:40:21* 1015826235 220003038067 *1004941621*
1015826235 *200002448035* 2001-11-11 22:21:11 1015826235 *210002448035* 1005542471
任何幫助將不勝感激。
更新: -
select * from (select * from (select user_id, prod_and_ts.product_id as
product_id, prod_and_ts.timestamps as timestamps from testingtable2 LATERAL VIEW
explode(purchased_item) exploded_table as prod_and_ts) prod_and_ts LEFT OUTER
JOIN table2 ON (prod_and_ts.user_id = table2.buyer_id AND table2.item_id =
prod_and_ts.product_id AND prod_and_ts.timestamps = UNIX_TIMESTAMP
(table2.created_time)) where table2.buyer_id IS NULL) set_a LEFT OUTER JOIN
table2 ON (set_a.user_id = table2.buyer_id AND (set_a.product_id =
table2.item_id OR set_a.timestamps = UNIX_TIMESTAMP(table2.created_time)));
你不是已經問過這個問題了嗎? [SQL查詢JOIN與表](http://stackoverflow.com/questions/11386368/sql-query-join-with-table) – 2012-07-09 00:11:55
我問它,但在我發佈Table1作爲一個單獨的SQL查詢,但在這裏我有通過將Table1和Table2分開以使人們不會感到困惑,簡化了它。 – ferhan 2012-07-09 00:13:57
然後,我會刪除您之前的問題,否則您將會以完全相同的方式關閉此問題。 – 2012-07-09 00:15:19