簡體   English   中英

在這種情況下如何編寫SQL語句?

[英]How to write SQL statement in this case?

由於某種原因,我想更改數據庫。

由於記錄太多,因此無法手動一一更改這些值。

請告知如何編寫此SQL,謝謝。

從:

 id  percentage    type
  1     6        services
  1     10       testing
  3     8        services
  3     20       testing
  6     61       services
  6     90       testing

至:

id percentage    type
1     10       services
1     10       testing
3     20       services
3     20       testing
6     90       services
6     90       testing

使用相關查詢,您可以這樣進行:

SELECT 
  t1.id,
  (SELECT MAX(t2.percentage) 
   FROM table1 t2 
   WHERE t2.id = t1.id) percentage,
  type
FROM Table1 t1

SQL小提琴演示

如果只想選擇,請參見Mahmoud Gamal的答案

但是,如果要永久更改該值,請使用UPDATE

UPDATE  tableName a
        INNER JOIN
        (
            SELECT id, MAX(percentage) AS maxPercentage
            FROM tableName
            GROUP BY id
        ) b ON a.ID = b.ID
SET a.percentage = b.maxPercentage;

編寫參考表以匹配服務和測試值之間的比例(請參見下面的示例),然后根據參考表的每一行編寫一個更新查詢:

id  service_val  testing_val
1       6        10
2       8        20
3       61       90

在此表上運行全選並在諸如php,python等之類的結果中進行迭代,將允許您動態替換mysql查詢中的值。 這種方法允許兩個值與其他答案沒有直接相關性(即:服務不必一定是測試的百分比)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM