我如何从多行中获取单个数据(短信问题)
How I get single data from multiple row (SMS issue)
我要制作聊天选项。我的数据库如下--
id from_sms to_sms body_sms time
1 1000 2000 hi timestamp
2 1000 2000 how r u? timestamp
3 2000 1000 fine. u? timestamp
4 1000 2000 I am fine. timestamp
5 3000 1000 hey r u there? timestamp
6 1000 3000 bg now. timestamp
7 4000 1000 I am new timestamp
这里-
1000 = 我 2000 = X 3000 = Y 4000 = Z
现在我想在我们的手机节目中只显示号码
Message
-------
2000
3000
4000
我如何执行我的要求?
您似乎想要显示给定用户与之交互的所有不同数字。使用 union
:
可能更简单、更有效
select from_sms as contact from mytable where to_sms = 1000
union -- on purpose: removes duplicates
select to_sms from mytable where from_sms = 1000
另一种方法是使用条件表达式:
select distinct case when from_sms = 1000 then to_sms else from_sms end as contact
from mytable
where 1000 in (from_sms, to_sms)
SELECT DISTINCT CASE WHEN from_sms = @my_sms
THEN to_sms
ELSE from_sms END AS contact
FROM source_table
我要制作聊天选项。我的数据库如下--
id from_sms to_sms body_sms time
1 1000 2000 hi timestamp
2 1000 2000 how r u? timestamp
3 2000 1000 fine. u? timestamp
4 1000 2000 I am fine. timestamp
5 3000 1000 hey r u there? timestamp
6 1000 3000 bg now. timestamp
7 4000 1000 I am new timestamp
这里- 1000 = 我 2000 = X 3000 = Y 4000 = Z
现在我想在我们的手机节目中只显示号码
Message
-------
2000
3000
4000
我如何执行我的要求?
您似乎想要显示给定用户与之交互的所有不同数字。使用 union
:
select from_sms as contact from mytable where to_sms = 1000
union -- on purpose: removes duplicates
select to_sms from mytable where from_sms = 1000
另一种方法是使用条件表达式:
select distinct case when from_sms = 1000 then to_sms else from_sms end as contact
from mytable
where 1000 in (from_sms, to_sms)
SELECT DISTINCT CASE WHEN from_sms = @my_sms
THEN to_sms
ELSE from_sms END AS contact
FROM source_table