0

允许作者将文章作为社区的一部分提交,并使用 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

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

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”
这是 CASE 的来源:
2 . 只有一个 author_id 的(免费)文章,会得到一个 article_title 'Good Read' 和一个 article_clean 'good-read'
3. 只有一个(免费)文章,如果对于一个 author_id,一个 article_title 'Good Read' 和一个 article_clean 'good-read' 已经存在,无论是来自 mysql 查询,还是已经存在于该 author_id 的文章表中,那么值 1 将被附加为:
    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。

4

1 回答 1

1

因为这是没有像 ROW_NUMBER 这样的分析函数的 MySQL,所以你必须使用 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;  
于 2012-09-16T10:13:18.317 回答