Mysql tablo için iyi güncelleme sorgusu

5 Cevap php

Ben kullanıcıya benzersiz her indirme için bir noktası sağlamak bir dosya barındırma sitesi var.

Sample of my table
Sample table

Bu noktalar kullanıcı tarafından itfa edilebilir. Yani, örneğin bir kullanıcı 100 puan azalır kadar her satır mevcut noktaları azaltmak için en iyi sorgu olandan 100 puan kurtarır eğer.

Teşekkür ederiz.

5 Cevap

Tamam, o zaman, burada ben bunu yapacağını SQL-dilsiz bir yoldur. Umarım bir SQL gurusu daha iyi bir çözüm gelecek. Note: Bu saf pseudocode; Bu dayanarak kendi kod yazmak - bu kutunun dışında işe gitmiyor.

$total_to_deduct = 100;

// Each time, get the row with the highest points
$top_points_query = "SELECT id, points FROM my_table ORDER BY points DESC LIMIT 1;"

do {
  $result = do_query($top_points_query);

  if($result) {
    // I'm assuming you don't want to deduct more points from a row than it has
    $num_to_deduct = min($result['points'], $total_to_deduct);

    // Now deduct the points from the row we got earlier
    $update_query = "UPDATE my_table SET points = points - $num_to_deduct
                     WHERE id = $result['id']";

    if(do_query($update_query)) {
      $total_to_deduct -= $num_to_deduct;
    }
  }
} while($total_to_deduct > 0); // If we still have points to deduct, do it again

Bunun için iki tablo oluşturmanız gerekir:

Table files
- id
- name
- size

Table points
- id
- file_id
(- user)
- points

Yeni bir dosya eklemek:

INSERT INTO files (name, size) VALUES ('kat92a.jpg', 105544); // New file with ID 1

Şimdi, bir dosyaya olumlu veya olumsuz puan verebilirsiniz:

INSERT INTO points (file_id, points) VALUES (1, 100); //Positive points
INSERT INTO points (file_id, points) VALUES (1, -10); //Negative points

Ve puan toplam sayısını seçebilirsiniz:

SELECT 
    files.name, 
    files.size, 
    (SELECT sum(points) FROM points WHERE file_id = 1) AS points 
FROM files
WHERE id = 1 

Eğer sadece basit bir güncelleme Bildirimi ihtiyaç gibi görünüyor ve satır güncellemek için izin verir ve eğer 100'den fazla güncelleme değil.

update table set points = if( (points+<VALUE>) <= 100,points+<VALUE>,points) where id = <FILE ID>  

Bu güncelleştirme bildirimi, sonucu dönecektir sonra ise puan, 100'den daha yüksek olup olmadığını görmek için kontrol eder. Değeri 100'den az ise, o zaman tablosunu güncelleştirmek ve güncellenmiş satırların miktarını size geri verecektir.

Sadece İntifa puan miktarı ile kullanıcı tabloda bir sütun ekleyebilirsiniz. Sizin için uygun bir çözüm olduğunu?

Burada saf SQL çözümdür, ama (a) bu denenmemiş ve (b) sadece bir kavram olduğunu sizi uyarır.

DECLARE curs CURSOR FOR
    SELECT
        id,
        points,
    FROM
        points
    WHERE
        points > 0;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET remPoints = 0;
OPEN curs;

SET remPoints = 100; /* modify this value, probably in your app */

REPEAT
    FETCH curs INTO cId, cPoints;

    IF remPoints >= cPoints THEN
        UPDATE points SET points = 0 WHERE id = cId;
    ELSE
        UPDATE points SET points = points - remPoints WHERE id = cId;
    END IF;
    SET remPoints = remPoints - cPoints;
UNTIL remPoints <= 0;

CLOSE curs;