【问题标题】:SQL Query return one of eachSQL 查询返回一个
【发布时间】:2014-11-03 21:44:57
【问题描述】:
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1
union all
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores 
on chat_b_users.id_participante1 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante2 = 1 order by last_active DESC

如何选择不同的值?

我需要返回所有这些数据,即使是空值,但是对于每个用户,我该怎么做?

结果:

正如你在图片中看到的,我有两个来自同一个用户的聊天,我只想要一个。

【问题讨论】:

  • 你试过SELECT DISTINCT吗?
  • 是的,我试过了,结果一样
  • 我明白了,DISTINCT 不起作用,因为组合不同。您的时间戳使它们没有区别。试试MAXlast_active 看看是否有帮助。看看this answer
  • 不工作,你想让我把它放在查询的什么地方?
  • 尝试 EXCEPT 而不是 UNION

标签: sql sql-server


【解决方案1】:

试试这个:在代码中添加列列表来确定要显示的行。

 SELECT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora
(
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora ,
ROW_NUMBER (PARTITION BY <add_yr_colist> ORDER BY Last_Avtive DESC) AS RNUM 
from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1 OR id_participante2 = 1
)TVC WHERE RNUM = 1

【讨论】:

  • 这是什么?
  • 添加将标识一个特定窗口的列列表。说在您的示例中,您需要为 user_Id = 40 显示消息 OK ,然后将 替换为 user_id ,这将在基于 user_id 值的不同窗口。希望这可以解释。
  • ROW_NUMBER(PARTITION BY User_id ORDER BY Last_Active DESC)
  • SELECT * ( select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora, ROW_NUMBER (PARTITION BY id_user ORDER BY last_active DESC) AS RNUM from chat_b_users 内部连接 ​​utilizadores在 chat_b_users.id_participante2 = utilizadores.id_user 离开加入 chat_talks on chat_b_users.id_chat = chat_talks.id_chat where id_participante1 = 1 OR id_participante2 = 1 )TVC WHERE RNUM = 1 |||还是有错误
  • 这次是我的错误:ROW_NUMBER 应该是这样 ROW_NUMBER() OVER (PARTITION BY id_user ORDER BY last_active DESC)
【解决方案2】:

您需要使用UNIION 而不是UNION ALL,如下所示:

select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1
union 
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores 
on chat_b_users.id_participante1 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante2 = 1 order by last_active DESC

UNION 删除重复记录(结果中的所有列都相同),UNION ALL 不会

Difference betwwen UNION & UNION ALL

【讨论】:

  • 同一个
  • @Severiano 如我所见,message and dataHora 与前一个不同。所以它显示了两次。
  • 它显示两次但不同的消息,如果我有 5 条消息,它将显示 5 次相同的用户,我只想要最后一个
  • @Severiano 桌子上的主键是什么
  • id_user 为主键
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
相关资源
最近更新 更多