我有4個表:如何選擇SQL中具有多個不同值的行?
** [PERSON]
ID_Person Person_NAME**
1 First name 1 Last name 1
2 First name 2 Last name 2
3 First name 3 Last name 3
4 First name 4 Last name 4
5 First name 5 Last name 5
6 First name 6 Last name 6
**[mobile_number]
ID Mobile_Number OPERATOR**
1 797900010 M
2 797900011 M
3 698900010 I
4 797900012 I
5 698900011 J
6 797900013 T
7 797900011 J
8 698900012 I
9 797900014 L
10 698900013 M
11 797900015 M
**[user_mobile]
ID ID_USER ID_MOBILE**
1 1 1
2 1 3
3 1 11
4 2 6
5 2 8
6 3 5
7 3 10
8 4 2
9 5 4
10 5 7
11 6 9
**[MESSAGE_ID]
ID_Message ID_MOBILE Message Date**
1 1 text 1 12/04/2011
2 1 text 2 07/07/2011
3 1 text 3 05/11/2011
4 2 text 4 01/13/2012
5 2 text 5 17/02/2012
6 2 text 6 13/12/2012
7 3 text 7 25/12/2011
8 4 text 8 11/11/2012
9 4 text 9 03/03/2012
10 5 text 10 30/04/2012
11 5 text 11 28/02/2012
12 6 text 12 01/06/2011
13 7 text 13 19/08/2010
14 8 text 14 22/12/2010
15 8 text 15 14/05/2010
16 9 text 16 09/04/2012
17 10 text 17 11/05/2011
18 11 text 18 15/01/2012
我必須選擇已在不同年份發來的短信要顯示 列的所有用戶:個人姓名,手機號碼,SMSText ,年
我的解決辦法:
Select a.Person_Name, Mobile_Nr, message, year(Date) as Years from PERSON a
inner join USER_MOBILE b on a.ID_PERSON=b.ID_USER
inner join MOBILE_NUMBER c on b.ID_MOBILE=C.ID
inner join MESSAGE_ID d on c.ID=d.ID_Mobile
group by Person_Name, Mobile_Nr,message,year(Date)
having count(distinct year(date))>1
但它不會返回任何內容。如果我把它改爲「having count(distinct year(date))=1
」我得到:
**Person_Name Mobile_Nr message Years**
Firstname 1 Lastname 1 698900010 text7 2011
Firstname 1 Lastname 1 797900010 text1 2011
Firstname 1 Lastname 1 797900010 text2 2011
Firstname 1 Lastname 1 797900010 text3 2011
Firstname 1 Lastname 1 797900015 text18 2012
Firstname 2 Lastname 2 698900012 text14 2010
Firstname 2 Lastname 2 698900012 text15 2010
Firstname 2 Lastname 2 797900013 text12 2011
Firstname 3 Lastname 3 698900011 text10 2012
Firstname 3 Lastname 3 698900011 text11 2012
Firstname 3 Lastname 3 698900013 text17 2011
Firstname 4 Lastname 4 797900011 text4 2012
Firstname 4 Lastname 4 797900011 text5 2012
Firstname 4 Lastname 4 797900011 text6 2012
Firstname 5 Lastname 5 698900009 text13 2010
Firstname 5 Lastname 5 797900012 text8 2012
Firstname 5 Lastname 5 797900012 text9 2012
Firstname 6 Lastname 6 797900014 text16 2012
但這是錯誤的,我想只顯示在不同年份發來的短信用戶。
你想有你的蛋糕和熊掌兼得。如果您只想顯示用戶,請不要顯示電話號碼或信息。 –