2

In a table of articles

title varchar(255),
category int(11),
processed enum('yes', 'no'),
... other columns

, I want to process rows (SELECT a row and then UPDATE). However, I need to do this diversely for all categories. Not processing randomly, e.g. all records for a category, but nothing for another.

  1. Basic Case: process x rows for each category.

  2. Advanced Case: define a daily limit for each category (in its table). This will be similar to crawlers, as we define how many pages should be crawled for a domain in a given period of time.

Example:

SELECT * from articles WHERE process='no' LIMIT 1
edit the columns in PHP
UPDATE articles .... WHERE id=xx (id comes from SELECT).

Table:

id    title    category  process
1     title1   3         no
2     title2   3         no
3     title3   3         no
4     title4   3         no
5     title5   5         no
6     title6   5         no
7     title7   5         no

If I run the query regularly by cron, it will process all articles in category 3 then category 5. I want a query to process one from category 3, then one from category 5, and so forth. I want to process from all categories gradually.

4

4 回答 4

1
SELECT *
FROM Table
WHERE category =
(SELECT category
FROM Table
WHERE process = 'no'
GROUP BY category
ORDER BY COUNT(category) DESC
LIMIT 1)
ORDER BY id
LIMIT 1

..将为您提供一个具有最小 id 的行,用于未处理的行数最多的类别。子查询返回具有最多 process='no' 行的类别。

如果您的 5s 多于 3s,这将继续给您 5s,直到 3s 多于 5s,然后它将开始与每个查询交替(只要您每次都将行标记为 process = 'yes')。

于 2012-05-17T17:44:19.553 回答
1

$n要从每个类别中进行选择:

SET @last := NULL;
SELECT * FROM (
  SELECT   *,
           @fetch:=IF(category=@last, @fetch-1, $n) x,
           @last :=category
  FROM     articles
  WHERE    process='no'
  ORDER BY category
) t WHERE t.x > 0;

要从表中为每个category关联选择:numbernumbers

SET @last := NULL;
SELECT * FROM (
  SELECT   *,
           @fetch:=IF(category=@last, @fetch-1, numbers.number) x,
           @last :=category
  FROM     articles JOIN numbers USING (category)
  WHERE    process='no'
  ORDER BY category
) t WHERE t.x > 0;

在sqlfiddle上查看它们。

于 2012-05-17T18:34:15.957 回答
0

更新eggyal的查询:为@n设置变量` SET @n := 3; SET @last := NULL;

SELECT * FROM ( SELECT *, @fetch:=IF(category=@last, @fetch-1, @n) x, @last :=category FROM 文章 WHERE process='no' ORDER BY category ) t WHERE tx > 0 ;

//查询运行`

于 2014-06-20T03:23:33.180 回答
0

我认为要在 PHP 中完成的处理是涉及用户编辑或一些 SQL 无法完成的复杂过程(爬网)的东西。在这种情况下,您可以使用此查询从表中获取所需的列articles

每个类别一篇文章:

SELECT 
    a.*
FROM 
    category AS c 
  JOIN
    articles AS a 
        ON  a.id = 
            ( SELECT id
              FROM articles AS aa
              WHERE category = c.id
                AND process = 'no'
              ORDER BY whatever
              LIMIT 1
            ) ;

然后更新:

UPDATE
    articles
SET
    process = 'yes'
  , other_column = ...
WHERE
    id = ?              --- one of the ids you have previously 
                        --- selected and processed.
于 2012-05-17T18:40:26.300 回答