1

我有这个查询,它将大量值添加到“统计”表中。当查询运行时,它会选择值以在子查询中填充表。我想知道这是否可以更有效地进行,或者我是否做错了什么。我对 MySQL 不太熟悉,所以任何帮助都会很棒:)

这是查询:

UPDATE mediastats SET 
    mediastats_members = (SELECT count(*) FROM status WHERE status_media_id = :id),
    mediastats_avscore = (SELECT AVG(status_rating) FROM status WHERE status_media_id = :id),
    mediastats_done = (SELECT count(*) FROM status WHERE status_status = 'done' AND status_media_id = :id),
    mediastats_doing = (SELECT count(*) FROM status WHERE status_status = 'doing' AND status_media_id = :id),
    mediastats_redoing = (SELECT count(*) FROM status WHERE status_status = 'redoing' AND status_media_id = :id),
    mediastats_dropped = (SELECT count(*) FROM status WHERE status_status = 'dropped' AND status_media_id = :id),
    mediastats_wantto = (SELECT count(*) FROM status WHERE status_status = 'wantto' AND status_media_id = :id),
    mediastats_wont = (SELECT count(*) FROM status WHERE status_status = 'wont' AND status_media_id = :id),
    mediastats_stalled = (SELECT count(*) FROM status WHERE status_status = 'stalled' AND status_media_id = :id),
    mediastats_rating_1 = (SELECT count(*) FROM status WHERE status_rating = 1 AND status_media_id = :id),
    mediastats_rating_2 = (SELECT count(*) FROM status WHERE status_rating = 2 AND status_media_id = :id),
    mediastats_rating_3 = (SELECT count(*) FROM status WHERE status_rating = 3 AND status_media_id = :id),
    mediastats_rating_4 = (SELECT count(*) FROM status WHERE status_rating = 4 AND status_media_id = :id),
    mediastats_rating_5 = (SELECT count(*) FROM status WHERE status_rating = 5 AND status_media_id = :id),
    mediastats_rating_6 = (SELECT count(*) FROM status WHERE status_rating = 6 AND status_media_id = :id),
    mediastats_rating_7 = (SELECT count(*) FROM status WHERE status_rating = 7 AND status_media_id = :id),
    mediastats_rating_8 = (SELECT count(*) FROM status WHERE status_rating = 8 AND status_media_id = :id),
    mediastats_rating_9 = (SELECT count(*) FROM status WHERE status_rating = 9 AND status_media_id = :id),
    mediastats_rating_10 = (SELECT count(*) FROM status WHERE status_rating = 10 AND status_media_id = :id)
    WHERE mediastats_media_id = :id

:id 是从 PHP 添加的。

4

2 回答 2

2

下面是我在 PHP 中使用 PDO 的方法:

$sql = "
    SELECT
     COUNT(*) AS mediastats_members,
     AVG(status_rating) AS mediastats_avscore,
     SUM(status_status = 'done') AS mediastats_done,
     SUM(status_status = 'doing') AS mediastats_doing,
     SUM(status_status = 'redoing') AS mediastats_redoing,
     SUM(status_status = 'dropped') AS mediastats_dropped,
     SUM(status_status = 'wantto') AS mediastats_wantto,
     SUM(status_status = 'wont') AS mediastats_wont,
     SUM(status_status = 'stalled') AS mediastats_stalled,
     SUM(status_rating = 1) AS mediastats_rating_1,
     SUM(status_rating = 2) AS mediastats_rating_2,
     SUM(status_rating = 3) AS mediastats_rating_3,
     SUM(status_rating = 4) AS mediastats_rating_4,
     SUM(status_rating = 5) AS mediastats_rating_5,
     SUM(status_rating = 6) AS mediastats_rating_6,
     SUM(status_rating = 7) AS mediastats_rating_7,
     SUM(status_rating = 8) AS mediastats_rating_8,
     SUM(status_rating = 9) AS mediastats_rating_9,
     SUM(status_rating = 10) AS mediastats_rating_10
    FROM status
    WHERE status_media_id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(array("id"=>$id));
$params = $stmt->fetch(PDO::FETCH_ASSOC);

通过这种方式,您可以一次计算表中的所有聚合,而不是为每个计数使用单独的子查询。

我正在使用 MySQL 的技巧——布尔表达式的 SUM() 等于表达式为真的 COUNT()。这是因为 MySQL 布尔表达式总是返回 0 或 1,并且 0 和 1 的 SUM 等于 1 的 COUNT。

然后,您可以将上述查询的结果用作 UPDATE 语句的参数数组:

$sql = "
    UPDATE mediastats SET 
        mediastats_members = :mediastats_members,
        mediastats_avscore = :mediastats_avscore,
        mediastats_done = :mediastats_done,
        mediastats_doing = :mediastats_doing,
        mediastats_redoing = :mediastats_redoing,
        mediastats_dropped = :mediastats_dropped,
        mediastats_wantto = :mediastats_wantto,
        mediastats_wont = :mediastats_wont,
        mediastats_stalled = :mediastats_stalled,
        mediastats_rating_1 = :mediastats_rating_1,
        mediastats_rating_2 = :mediastats_rating_2,
        mediastats_rating_3 = :mediastats_rating_3,
        mediastats_rating_4 = :mediastats_rating_4,
        mediastats_rating_5 = :mediastats_rating_5,
        mediastats_rating_6 = :mediastats_rating_6,
        mediastats_rating_7 = :mediastats_rating_7,
        mediastats_rating_8 = :mediastats_rating_8,
        mediastats_rating_9 = :mediastats_rating_9,
        mediastats_rating_10 = :mediastats_rating_10
    WHERE mediastats_media_id = :id";

$stmt = $pdo->prepare($sql);
$params["id"] = $id;
$stmt->execute($params);

自 PHP 5.3.4 起,PDO 接受不带前导的参数数组键:。在查询中声明参数占位符时需要冒号,但在提供给 execute() 的值数组中不需要冒号。

于 2013-04-16T17:05:15.813 回答
1
UPDATE  (
        SELECT  status_media_id,
                COUNT(*) AS cnt, AVG(status_rating) AS avg_rating,
                SUM(status_status = 'done') AS cnt_done,
                ...
        FROM    status
        WHERE   status_media_id = :id
        ) s
JOIN    mediastats ms
ON      ms.mediastats_media_id = s.status_media_id
SET     ms.mediastats_members = cnt,
        ms.mediastats_avscore = avg_rating,
        ms.mediastats_done = cnt_done,
        ...
于 2013-04-16T16:56:04.860 回答