新人~~求助一个问题~~谢谢

icyfantasy 2011-04-01 04:22:25
假设我有两个表:User(user_id, date_of_birth, city_id) 和 Message(message_id, message_type, posted)
想要删除Message表中年龄低于21岁且city_id=1发帖时间超过一年的公共留言(message_id=1)
但是遇到了问题,我试了试只能确定错在哪了(红字),但不知道该怎么做,希望达人帮忙,最好可以不使用subquery

delete from Message
USING Message,User
where
owner_id =(select user_id from User
where datediff(curdate(),date(date_of_birth))<365*21
)

AND
message_type=1
AND
datediff( curdate(),date(posted) )>365
AND
city_id=1

...全文
58 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2011-04-01
  • 打赏
  • 举报
回复
delete Message from Message m , User n
where m.owner_id = n.user_id and
city_id=1 and message_id = 1 and
(datediff(yy,posted ,getdate()) > 1 or (datediff(yy,posted ,getdate()) = 1 and right(convert(varchar(10),posted,120),5) > right(convert(varchar(10),getdate(),120),5)))
(datediff(yy,date_of_birth,getdate()) < 21 or (datediff(yy,date_of_birth ,getdate()) = 21 and right(convert(varchar(10),date_of_birth,120),5) < right(convert(varchar(10),getdate(),120),5)))


你那个求一年,或是求21岁以下的不能那么做.
Hankuker 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wufeng4552 的回复:]
SQL code
delete from Message
USING Message,User
where

神马意思?
[/Quote]
++
dawugui 2011-04-01
  • 打赏
  • 举报
回复
delete from Message where city_id=1 and message_id = 1 and 
(datediff(yy,posted ,getdate()) > 1 or (datediff(yy,posted ,getdate()) = 1 and right(convert(varchar(10),posted,120),5) > right(convert(varchar(10),getdate(),120),5)))
and owner_id = (select user_id from user where datediff(yy,date_of_birth,getdate()) < 21 or (datediff(yy,date_of_birth ,getdate()) = 21 and right(convert(varchar(10),date_of_birth,120),5) < right(convert(varchar(10),getdate(),120),5)))
水族杰纶 2011-04-01
  • 打赏
  • 举报
回复
delete from Message
USING Message,User
where

神马意思?
AcHerat 2011-04-01
  • 打赏
  • 举报
回复
user_id可能会有多个,不能=,用in试试!
duoxu1983 2011-04-01
  • 打赏
  • 举报
回复
=变成in
xuam 2011-04-01
  • 打赏
  • 举报
回复

owner_id in (select user_id from User
where datediff(curdate(),date(date_of_birth))<365*21
)

--小F-- 2011-04-01
  • 打赏
  • 举报
回复
delete from
Message USING Message,User
where
owner_id =(select user_id from User where datediff(dd,curdate(),date(date_of_birth))<365*21
)
AND
message_type=1
AND
datediff( dd,curdate(),date(posted) )>365
AND
city_id=1

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧