Sql 有从句是行不通的
我是SQL的初学者,我目前正在尝试使用HAVING子句,但它不起作用 我有两张桌子:Sql 有从句是行不通的,sql,group-by,having,Sql,Group By,Having,我是SQL的初学者,我目前正在尝试使用HAVING子句,但它不起作用 我有两张桌子: tchat: tchat_信息: 所以我想从用户那里得到最新的消息 首先:我加入表格:` select user_id, user_message, max(date_message) from tchat inner join tchat_message on tchat.id=tchat_message.user_id 给你 Second:我使用having子句: select user_id,
select user_id, user_message, max(date_message)
from tchat
inner join tchat_message on tchat.id=tchat_message.user_id
给你
Second:我使用having子句:
select user_id, user_message, max(date_message)
from tchat
inner join tchat_message on tchat.id=tchat_message.user_id
group by user_id
having max(date_message) = date_message`
这里我有一个错误,它说:
“having子句”中的未知列“date\u message”
有人有想法吗?您使用的
having
子句不正确。以下内容更接近您想要的内容:
select tm.user_id, tm.user_message, tm.date_message
from tchat t inner join
tchat_message tm
on t.id = tm.user_id inner join
(select user_id, max(date_message) as maxdm
from tchat_message
group by user_id
) tmm
on tmm.user_id = tm.user_id and tmm.maxdm = tm.date_message;
您不需要在外部查询中使用
分组依据
。您似乎也不需要tchat
表(我保留了它,以防您在select
表中有其他列).没有比这更简单的了?@Mr.Smith67请在重复问题中查看其他技巧。@GordonLinoff:有时,如果您只想在另一个表中包含具有匹配项的记录,即使您没有从表中选择列,也有必要连接到表中。