一个SQL语句的问题。高手帮帮忙

金拓 2009-11-10 05:25:16
问题是这样的。
有3个表。
表1有字段id typeid cateid content
表2有字段typeid name
表3有字段cateid name
现在表1的typeid没有值,cateid有值,对应着表3的cateid
我现在想要更新表1中的typeid的值,要求是,表3里的name和表2里的name相等的typeid更新给表1的typeid。
比如:
表1里有一行是
id typeid cateid content
1 0 1 内容

表3中cateid=1的name是法国
表2中typeid=3的name是法国。
更新后,表1变成
id typeid cateid content
1 3 1 内容
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cayleung 2009-11-10
  • 打赏
  • 举报
回复
..............
update 不能用from的
都认真看看手册吧
金拓 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhoupuyue 的回复:]
update t1,t2,t3 set t1.typeid=t2.typeid
where t2.name=t3.name and t1.cateid=t3.cateid
[/Quote]
好用,谢谢:)
dzxccsu 2009-11-10
  • 打赏
  • 举报
回复
update t1 set t1.typeid=t2.typeid from t1,t2,t3 where t1.cateid=t3.cateid and t2.name=t3.name 
阿_布 2009-11-10
  • 打赏
  • 举报
回复
update t1,t2,t3 set t1.typeid=t2.typeid
where t2.name=t3.name and t1.cateid=t3.cateid
金拓 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oh_jf 的回复:]
update t1 set typeid=t2.typeid from t1,t2,t3 where t1.cateid=t3.cateid and t2.name=t3.name

[/Quote]

SQL 查询:

UPDATE cdb_threads SET typeid = cdb_threadtypes.typeid FROM cdb_threads,
cdb_threadtypes,
cdb_threadcates WHERE cdb_threads.cateid = cdb_threadcates.cateid AND cdb_threadtypes.name = cdb_threadcates.name

MySQL 返回:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from
cdb_threads,cdb_threadtypes,cdb_threadcates
where
cdb_threads.cateid=c' at line 5
oh_jf 2009-11-10
  • 打赏
  • 举报
回复
update t1 set typeid=t2.typeid from t1,t2,t3 where t1.cateid=t3.cateid and t2.name=t3.name

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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