PDO,MySQL-使用单个CASE有效地更新/增加数组中的值



作者可以作为社区的一部分提交文章,并通过PDO查询将其保存到mysql数据库中。让我们用7篇文章来扩展这个例子:

article_id   author_id   article_title   article_clean   article_kind   good_read
    1            2       War and Peace   war-and-peace     free            y
    2            3       Art of War      art-of-war        free            y
    3            2       Peace and Love  peace-and-love    paid            n
    4            4       Peaceful Living peaceful-living   paid            n
    5            2       Peace Treaties  peace-treaties    free            n
    6            2       Peace is Love   peace-is-love     free            n
    7            4       Peace Countries peace-countries   free            n

我已经有了文章ids:的输出

while($row = $articles->fetch(PDO::FETCH_ASSOC)){           
    $article_id_array[] = $row["article_id"];
    $author_id_array[] = $row["author_id"];
    $article_clean_array[] = $row["article_clean"];
    $article_kind_array[] = $row["article_kind"];
}

我试图运行一个mysql命令,也许使用CASE,它只会影响这样的文章:
1.article_id_array中的所有(免费或付费)文章都将获得一个"y"
以下是案例的来源:
2.只有单个author_id的(免费)文章才会获得article_title"Good Read"和article_clean nbsp '阅读良好'
3.只有(免费)文章,如果是单个author_id,则为article_title"Good Read"和article_clean"Good Read'" nbsp 已经存在,无论是来自mysql查询,还是已经存在于author_id的articles表中,那么 nbsp 值1将按如下方式附加:
 nbsp nbsp;article_title"Good Read 1"和article_clean"Good-Read-1"

因此,在上面的例子中,在查询之后,articles表将如下所示:

article_id   author_id   article_title   article_clean   article_kind   good_read
    1            2       Good Read       good-read         free            y
    2            3       Good Read       good-read         free            y
    3            2       Peace and Love  peace-and-love    paid            y
    4            4       Peaceful Living peaceful-living   paid            y
    5            2       Good Read 1     good-read-1       free            y
    6            2       Good Read 2     good-read-2       free            y
    7            4       Good Read       good-read         free            y

你知道用一个PDO查询来完成这项任务是什么吗

编辑:
我正在寻找最有效的方法来做到这一点,因为这个表可能增长得很快(数十万篇文章),所以我想要的查询不会削弱数据库。作为参考,这不会针对数据库中的每一篇文章,一次只为脚本提供20-100个文章id来执行查询。

由于这是MySQL,没有像ROW_NUMBER这样的分析函数,您必须使用OUTER JOIN来模拟它——顺便说一句,这在性能方面不是一个好的选择。如果我正确理解任务,您可以通过运行以下查询来实现:

 SELECT article_id,
       author_id,
       CASE
         WHEN article_kind = 'free' THEN
           CASE
             WHEN num = 1 THEN 'Good Read'
             ELSE Concat('Good Read ', Cast((num - 1) AS CHAR))
           end
         WHEN article_kind = 'paid' THEN article_title
       end AS article_title,
       CASE
         WHEN article_kind = 'free' THEN
           CASE
             WHEN num = 1 THEN 'good-read'
             ELSE Concat('good-read-', Cast((num - 1) AS CHAR))
           end
         WHEN article_kind = 'paid' THEN article_title
       end AS article_clean,
       article_kind,
       'y' AS good_read
FROM   (SELECT a.article_id,
               a.author_id,
               a.article_title,
               a.article_clean,
               a.article_kind,
               a.good_read,
               Count(*) AS num
        FROM   articles AS a
               LEFT OUTER JOIN articles AS b
                            ON a.author_id = b.author_id
                               AND a.article_id >= b.article_id
                               AND b.article_kind = 'free'
        GROUP  BY article_id,
                  author_id,
                  article_title,
                  article_clean,
                  article_kind,
                  good_read) AS tab;  

最新更新