1

我有以下数据

 id |   bill_of_lading |  shipping_rate | 
____|__________________|________________|
   1|              8012|              90|
   2|              6045|              90|
   3|              6045|               0|
   4|              2423|               0|
   5|              5342|              90|
   6|              5643|              90|
   7|              5643|               0|
   8|              7545|               0|
   9|              9034|               0|

我想返回仅出现 1 次 bill_of_lading 编号的行,并使用结果更新运费为零的记录上的 shipping_rate。

例如,我想在 ID {4、8、9} 上将运费设置为 ​​220,但在 ID {3、7} 上不设置

我完全不确定用于这样的场景的语法。

4

3 回答 3

3
SELECT bill_of_lading, COUNT(*)
FROM t
GROUP BY bill_of_lading
HAVING COUNT(*) = 1

在行动中看到它:http ://sqlfiddle.com/#!2/842d0/2

要使用结果来更新相应的行,只需加入它:

UPDATE t
JOIN (
    SELECT bill_of_lading, COUNT(*)
    FROM t
    GROUP BY bill_of_lading
    HAVING COUNT(*) = 1
) AS selection USING (bill_of_lading)
SET shipping_rate = 220

看看那个在行动:http ://sqlfiddle.com/#!2/87b61/1

于 2013-01-04T06:09:41.397 回答
1

试试这个:

UPDATE tablename 
SET shipping_rate = 220 
WHERE shipping_rate = 0 AND 
      id IN (SELECT id FROM (SELECT id FROM tablename 
             GROUP BY bill_of_lading HAVING COUNT(id) = 1) as A)
于 2013-01-04T06:12:45.763 回答
0

试穿这个尺寸,其他人可能有更优雅的东西

SELECT *, COUNT(*) AS total FROM table_name GROUP BY bill_of_lading HAVING total = 1
于 2013-01-04T06:09:02.540 回答