0

トリガーに次のものがあり、NULLが返されたときにゼロを挿入したい。ここ 2 日間、Stackoverflow やその他のサイトでさまざまな投稿を読みましたが、自分が望むものを達成できませんでした...そして、それが可能ではないのではないかと心配しています...? このクエリは、MCVMoodle.mdl_facetoface_signups.userid から、グレードのカウント値 (WHERE 条件を満たす) のすべてのユーザーを返します。カウント値は 1 以上ですが、0/null の場合は何もありません。結果なし/nullで作業することの難しさは理解していますが、カウントが0/nullのときにゼロを挿入する方法はありますか?

INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)

SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
      COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
        FROM
            MCVMoodle.mdl_facetoface_signups
        LEFT JOIN
            MCVMoodle.mdl_facetoface_signups_status on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
       WHERE
    MCVMoodle.mdl_facetoface_signups_status.statuscode = 80
      AND  MCVMoodle.mdl_facetoface_signups_status.superceded = 0
      AND  MCVMoodle.mdl_facetoface_signups_status.grade = 0
       GROUP BY
        MCVMoodle.mdl_facetoface_signups.userid;

以下は、最終的に私のために働いたクエリです。これについて重要な助けをしてくれたゴードンに感謝します。

INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)

SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
             COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
      FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
           MCVMoodle.mdl_facetoface_signups_status
           on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid and
              MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
              MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
              MCVMoodle.mdl_facetoface_signups_status.grade = 0
      GROUP BY MCVMoodle.mdl_facetoface_signups.userid
      ON DUPLICATE KEY UPDATE
       datab=VALUES(datab);
4

1 に答える 1

1

私の理解が正しければ、クエリからすべてを挿入する必要があります。ただし、クエリが空の場合は、すべてNULLの s の行を挿入します。以下はこれを行いますが、MySQL では行いません:

with cte as (
      SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
             COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
      FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
           MCVMoodle.mdl_facetoface_signups_status
           on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
      WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
            MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
            MCVMoodle.mdl_facetoface_signups_status.grade = 0
      GROUP BY MCVMoodle.mdl_facetoface_signups.userid
     )
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
    select *
    from cte
    union all
    select NULL, NULL, NULL
    where not exists (select * from CTE);

MySQL のバージョンはより複雑です。

INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
    select *
    from (SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
                 COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
          FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
               MCVMoodle.mdl_facetoface_signups_status
               on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
          WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
                MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
                MCVMoodle.mdl_facetoface_signups_status.grade = 0
          GROUP BY MCVMoodle.mdl_facetoface_signups.userid
         ) cte
    union all
    select NULL, NULL, NULL
    where not exists (SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
                             COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
                      FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
                           MCVMoodle.mdl_facetoface_signups_status
                           on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid
                      WHERE MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
                            MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
                            MCVMoodle.mdl_facetoface_signups_status.grade = 0
                      GROUP BY MCVMoodle.mdl_facetoface_signups.userid
            );

編集:

where句によって除外されたものも含めて、クエリからのすべての組み合わせを含めたいと思うかもしれません。その場合は、条件をon句に移動します。

with cte as (
      SELECT MCVMoodle.mdl_facetoface_signups.userid as 'userid', 4 as 'fieldid',
             COUNT(MCVMoodle.mdl_facetoface_signups_status.grade) as 'datab'
      FROM MCVMoodle.mdl_facetoface_signups LEFT JOIN
           MCVMoodle.mdl_facetoface_signups_status
           on MCVMoodle.mdl_facetoface_signups.id = MCVMoodle.mdl_facetoface_signups_status.signupid and
              MCVMoodle.mdl_facetoface_signups_status.statuscode = 80 AND
              MCVMoodle.mdl_facetoface_signups_status.superceded = 0 AND
              MCVMoodle.mdl_facetoface_signups_status.grade = 0
      GROUP BY MCVMoodle.mdl_facetoface_signups.userid
     )
INSERT INTO MCVMoodle.mdl_user_info_data_f2fskipcount (userid, fieldid, datab)
    select *
    from cte;
于 2013-09-10T02:20:04.287 に答える