2009-06-13 53 views
0

嘗試選擇全部爲同一客戶但地址不同的記錄。選擇相同的客戶名稱,但具有不同的客戶地址

因此,我可以稍後讓用戶選擇Bob Yonkers,然後選擇將Bob的所有記錄更新爲特定地址。所以我想顯示所有可用的記錄。

數據舉例:

 
CUSTOMER_NAME, CUSTOMER_ADDRESS 
Bob Yonkers , 42 Satellite Cir 
Bob Yonkers , 667 Orbit St 
Bob Yonkers , 42 Satellite Cir 
Bob Yonkers , 667 Orbit St 
David Boom , 5959 Bush Ave 
David Boom , 5959 Bush Ave 
David Boom , 5959 Bush Ave 
David Boom , 5959 Bush Ave 
David Boom , 5959 Bush Ave 
Ruby Tuesday , 123 Highway Ln Apt#1 
Ruby Tuesday , 123 Highway Ln 
David Boom ,5959 Bush Ave 
David Boom ,5959 Bush Ave 
David Boom ,5959 Bush Ave 

所以查詢將帶回這些結果...

結果舉例:

 
CUSTOMER_NAME, CUSTOMER_ADDRESS 
Bob Yonkers , 42 Satellite Cir 
Bob Yonkers , 667 Orbit St 
Ruby Tuesday , 123 Highway Ln Apt#1 
Ruby Tuesday , 123 Highway Ln 

任何幫助,將不勝感激。

+0

閱讀你的問題,好像你問* *的問題是不是你想要*問題*它的內容,如果你。希望找到有多個客戶駐留在該地址的地址。你能澄清一點嗎? – 2009-06-13 23:27:24

+0

請發表相關表格的結構 – eKek0 2009-06-13 23:27:24

+0

希望有幫助,沒有意識到它之前格式化得有多糟糕 – Mastro 2009-06-13 23:40:26

回答

2

這是喬爾的改進:

SELECT distinct t1.* 
FROM [table] t1 
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address 
5
SELECT * 
FROM [table] t1 
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address 
0

試試這個...

select * from (select count(customername) as ct, customername, address from table group by customername, address) t1 
where t1.ct>1 
0

這吸引了我,因爲一個朋友問我類似的東西。下面的查詢將解決這個問題,儘管在-有效:

mysql> select DISTINCT CUSTOMER_NAME,CUSTOMER_ADDRESS from CUST_ADDR where CUSTOMER_NAME in (select CUSTOMER_NAME from CUST_ADDR GROUP BY CUSTOMER_NAME HAVING COUNT(DISTINCT CUSTOMER_ADDRESS) > 1);

+---------------+----------------------+ 
| CUSTOMER_NAME | CUSTOMER_ADDRESS  | 
+---------------+----------------------+ 
| Bob Yonkers | 42 Satellite Cir  | 
| Bob Yonkers | 667 Orbit St   | 
| Ruby Tuesday | 123 Highway Ln Apt#1 | 
| Ruby Tuesday | 123 Highway Ln  | 
+---------------+----------------------+ 
4 rows in set (0.01 sec) 

相關問題