2015-06-26 107 views
1
Table 1 
    ID Customer Tracking 
    -------------------- 
    1 Nick  434 
    2 Jhon  437 
    3 Nick  456 
    4 Mike  544 

Table 2 
B_ID Customer Process 
---------------------- 
1 Nick  Payment 
2 Tom  Payment 
3 Nick  Payment 
4 Mike  Payment 
5 Nick  Payment 

    Echo 
    Table 1 Table 2 
    ------------------ 
    Nick 2 Nick 3 
    Mike 1 Mike 1 

如何僅從兩個不同的表和計數行中選擇simillar值。 正如你所看到的,我們在表1中有2個Nick,在表2中有3個Nick。兩個MYSQL表比較唯一行數

所以我想只顯示來自兩個表的類似客戶。

+0

在兩個表之間,您可以加入匹配值以獲取匹配數據的列表。 – Telavian

+0

[SQL連接中的過濾器重複]的可能重複(http://stackoverflow.com/questions/3029787/filter-duplicates-in-sql-join) – kixorz

回答

0

你可以count每個表(由名稱分組),並加入結果:

SELECT customer1, cnt1, customer2, cnt2 
FROM (SELECT customer AS customer1, COUNT(*) AS cnt1 
     FROM  table1 
     GROUP BY customer) t1 
JOIN (SELECT customer AS customer2, COUNT(*) AS cnt2 
     FROM  table2 
     GROUP BY customer) t2 ON customer1 = customer2 
+0

什麼是客戶1和客戶2? – user3391807

+0

分別來自table1和table2的客戶。它們是每個子查詢中'AS'子句定義的別名。 – Mureinik

+0

結果:每個派生表都必須具有自己的別名 – user3391807

0

寫的子查詢即得到每個表中的客戶數,然後加入他們的行列。

SELECT t1.customer, t1.count AS Table1, t2.count AS Table2 
FROM (SELECT customer, COUNT(*) AS count 
     FROM Table1 
     GROUP BY customer) AS t1 
JOIN (SELECT customer, COUNT(*) AS count 
     FROM Table2 
     GROUP BY customer) AS t2 
ON t1.customer = t2.customer