Sqlite 如何在特定位置将列的所有值设置为最大值?

Sqlite 如何在特定位置将列的所有值设置为最大值?,sqlite,sql-update,correlated-subquery,Sqlite,Sql Update,Correlated Subquery,如果我有: 2 baskets of oranges with 7 and 10 each 3 baskets of peaches with 12 and 15 each 然后我想设置: for every orange basket value of maxfruit to 10 and for every peach basket value of maxfruit to 15 我试过了 update baskets set maxfruit = (select max(f

如果我有:

 2 baskets of oranges with 7 and 10 each

 3 baskets of peaches with 12 and 15 each
然后我想设置:

for every orange basket value of maxfruit to 10 and 

for every peach basket value of maxfruit to 15
我试过了

 update baskets set maxfruit = (select max(fruitCount) from baskets b where b.fruit = fruit)   

但它只是将所有内容设置为15…

您的更新只是从整个表中提取最大值,您可以使用子查询提取每个水果的最大值

UPDATE b 
SET b.maxfruit = b2.fruitCount 
FROM baskets b 
INNER JOIN (SELECT fruit, MAX(fruitCount) AS fruitCount
            FROM baskets
            GROUP BY fruit) b2 ON b.fruit = b2.fruit

在SQL中,当按列的名称引用列时,除非使用表前缀,否则最终得到的表实例是最内层的表实例

所以水果指的是最里面的例子,b。这意味着b.水果和水果总是相同的值

要引用外部表实例,必须使用外部表的名称:

update baskets
set maxfruit = (select max(fruitCount)
                from baskets b
                where b.fruit = baskets.fruit);
                                ^^^^^^^^

你可以不写水果,只写水果,但这可能还不清楚。

尝试一下:更新篮子集maxfruit=从篮子b中选择maxfruit Count,其中b.fruit=水果,其中basket.fruit='fruit'关键是不要使用特定的名称:然后在插入数据时必须将其maxfruit值设置为max。我不确定这在SQLite中是否有效。它只是在“.”处返回一个错误,这就是T-SQL,您应该能够在SQLlite中执行类似的操作