SQL记录

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
2008年8月2日 | 标签:

删除重复数据:

id 为主键 name 为有重复的字段

  1. DELETE FROM table1 
  2. WHERE id NOT IN (SELECT MIN(id) FROM table1 GROUP BY name)

用其他表中的值更新表:

table1为要更新的表,id为主键,score为要被更新的字段
table1和table2的结构都为
id score

以table2中对应id的score来更新table1中对应id的score。

MySQL

  1. UPDATE table1 t1 
  2. SET  t1.score = (SELECT t2.score FROM table2 t2 WHERE t2.id=t1.id)
  3. WHERE EXISTS (SELECT NULL FROM table2 t2 WHERE t2.id=t1.id)

SQL Server

  1. UPDATE t1
  2. SET t1.score = t2.score
  3. FROM table1 t1,table2 t2
  4. WHERE t2.id=t1.id

貌似MSSQL的要比MySQL的简单一些……

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
目前还没有任何评论.