在SQLite中使用子查询更新表

前端之家收集整理的这篇文章主要介绍了在SQLite中使用子查询更新表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用ALTER TABLE和UPDATE语句向我的表中添加一列,而不是重新创建完整的表.

在我的UPDATE语句中使用子查询时,我没有得到我期望的输出.

建立可重复的数据

  1. library(dplyr)
  2. library(dbplyr)
  3. library(DBI)
  4. con <- DBI::dbConnect(Rsqlite::sqlite(),path = ":memory:")
  5. copy_to(con,iris[c(1,2,51),],"iris")
  6.  
  7. tbl(con,"iris")
  8. # # Source: table<iris> [?? x 5]
  9. # # Database: sqlite 3.19.3 []
  10. # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  11. # <dbl> <dbl> <dbl> <dbl> <chr>
  12. # 1 5.1 3.5 1.4 0.2 setosa
  13. # 2 4.9 3.0 1.4 0.2 setosa
  14. # 3 7.0 3.2 4.7 1.4 versicolor

在单独的表中创建新列

  1. DBI::dbSendQuery(con,"CREATE TABLE new_table AS SELECT t2.new_col from
  2. iris t1 inner join
  3. (SELECT Species,sum(`Sepal.Width`) as new_col FROM iris GROUP BY Species) t2
  4. on t1.Species = t2.Species")
  5.  
  6. tbl(con,"new_table")
  7. # # Source: table<new_table> [?? x 1]
  8. # # Database: sqlite 3.19.3 []
  9. # new_col
  10. # <dbl>
  11. # 1 6.5
  12. # 2 6.5
  13. # 3 3.2

在旧表中创建新列

  1. DBI::dbSendQuery(con,"ALTER TABLE iris ADD COLUMN new_col DOUBLE")

尝试从new_table插入新列

  1. DBI::dbSendQuery(con,"UPDATE iris SET new_col = (SELECT new_col FROM new_table)")
  2.  
  3. tbl(con,"iris")
  4. # # Source: table<iris> [?? x 6]
  5. # # Database: sqlite 3.19.3 []
  6. # Sepal.Length Sepal.Width Petal.Length Petal.Width Species new_col
  7. # <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
  8. # 1 5.1 3.5 1.4 0.2 setosa 6.5
  9. # 2 4.9 3.0 1.4 0.2 setosa 6.5
  10. # 3 7.0 3.2 4.7 1.4 versicolor 6.5

正如你所看到的,我的new_col只包含值6.5,我希望在最后一行有3.2.我怎样才能解决这个问题 ?

sql数据库中表中的行没有固有顺序.所以你不能像在R中那样分配值的“向量”.但是,你可以稍微修改你的查询
  1. library(dplyr)
  2. library(DBI)
  3. con <- DBI::dbConnect(Rsqlite::sqlite(),"iris")

使用聚合数据创建单独的表

  1. DBI::dbSendQuery(con,"CREATE TABLE new_table AS
  2. SELECT Species,sum(`Sepal.Width`) as new_col FROM iris GROUP BY Species")
  3.  
  4. tbl(con,"new_table")
  5. #> # Source: table<new_table> [?? x 2]
  6. #> # Database: sqlite 3.22.0 []
  7. #> Species new_col
  8. #> <chr> <dbl>
  9. #> 1 setosa 6.5
  10. #> 2 versicolor 3.2

在旧表中创建新列

  1. DBI::dbSendQuery(con,"ALTER TABLE iris ADD COLUMN new_col DOUBLE")

使用相关子查询将数据移动到原始表

  1. DBI::dbSendQuery(con,"UPDATE iris SET new_col = (SELECT new_col FROM new_table t2
  2. WHERE iris.Species = t2.Species)")
  3.  
  4. tbl(con,"iris")
  5. #> # Source: table<iris> [?? x 6]
  6. #> # Database: sqlite 3.22.0 []
  7. #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species new_col
  8. #> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
  9. #> 1 5.1 3.5 1.4 0.2 setosa 6.5
  10. #> 2 4.9 3 1.4 0.2 setosa 6.5
  11. #> 3 7 3.2 4.7 1.4 versicolor 3.2

如果你有多个计算列,你可以像这样使用UPDATE … SET(c1,c2,…)=(…):

  1. library(dplyr)
  2. library(dbplyr)
  3. library(DBI)
  4. con <- DBI::dbConnect(Rsqlite::sqlite(),"iris")
  5.  
  6. DBI::dbSendQuery(con,"CREATE TABLE aggs AS
  7. SELECT Species,SUM(`Sepal.Width`) AS sw_sum,AVG(`Sepal.Width`) AS sw_avg
  8. FROM iris GROUP BY Species")
  9. tbl(con,"aggs")
  10. #> # Source: table<aggs> [?? x 3]
  11. #> # Database: sqlite 3.22.0 []
  12. #> Species sw_sum sw_avg
  13. #> <chr> <dbl> <dbl>
  14. #> 1 setosa 6.5 3.25
  15. #> 2 versicolor 3.2 3.2
  16.  
  17. DBI::dbSendQuery(con,"ALTER TABLE iris ADD COLUMN sw_sum DOUBLE")
  18. DBI::dbSendQuery(con,"ALTER TABLE iris ADD COLUMN sw_avg DOUBLE")
  19.  
  20. DBI::dbSendQuery(con,"UPDATE iris
  21. SET (sw_sum,sw_avg) = (SELECT sw_sum,sw_avg
  22. FROM aggs WHERE iris.Species = aggs.Species)")
  23.  
  24. tbl(con,"iris")
  25. #> # Source: table<iris> [?? x 7]
  26. #> # Database: sqlite 3.22.0 []
  27. #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species sw_sum sw_avg
  28. #> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
  29. #> 1 5.1 3.5 1.4 0.2 setosa 6.5 3.25
  30. #> 2 4.9 3 1.4 0.2 setosa 6.5 3.25
  31. #> 3 7 3.2 4.7 1.4 versico… 3.2 3.2

这也适用于Postgres,但可能不适用于sql Server.

实际上,在这种情况下,不需要中间表:

  1. library(dplyr)
  2. library(dbplyr)
  3. library(DBI)
  4. con <- DBI::dbConnect(Rsqlite::sqlite(),sw_avg) =
  5. (SELECT sw_sum,sw_avg FROM
  6. (SELECT Species,AVG(`Sepal.Width`) AS sw_avg
  7. FROM iris GROUP BY Species) aggs
  8. WHERE iris.Species = aggs.Species)")
  9.  
  10. tbl(con,"iris")
  11. #> # Source: table<iris> [?? x 7]
  12. #> # Database: sqlite 3.22.0 []
  13. #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species sw_sum sw_avg
  14. #> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
  15. #> 1 5.1 3.5 1.4 0.2 setosa 6.5 3.25
  16. #> 2 4.9 3 1.4 0.2 setosa 6.5 3.25
  17. #> 3 7 3.2 4.7 1.4 versico… 3.2 3.2

但是,中间表在其他情况下可能会有所帮助.例如,在链接问题中使用R创建它时.

猜你在找的Sqlite相关文章