4

如何编写一个 MySQL 查询来完成这个任务?

表:作家

w_id    w_name
---------------
  1     Michael
  2     Samantha
  3     John
---------------

表:文章

a_id   w_id   timestamp   a_name
----------------------------------------
  1      1        1       PHP programming
  2      3        3       Other programming languages
  3      3        5       Another article
  4      2       15       Web design
  5      1       20       MySQL
----------------------------------------

只需要计算那些发表第一篇文章不早于5的作者(只统计至少发表过一篇文章的作者)


在此示例中,结果将是:1 (一位作家 - Samantha)

可以在此 SQLFiddle中测试 SQL 代码

4

8 回答 8

3

You need a double SELECT.

First you fetch w_id, together with timestamp of first article:

SELECT w_id, MIN(timestamp) as publish FROM articles GROUP BY w_id

Then you request that publish is not earlier than 5:

SELECT w_id, MIN(timestamp) as publish
    FROM articles
    GROUP BY w_id
    HAVING publish >= 5;

Then you join that "table" with writers to get the name, if you want.

But if you only want the count, you do not need the writers table at all:

    SELECT COUNT(*) AS answer FROM
    ( SELECT w_id, MIN(timestamp) AS publish
        FROM articles
        GROUP BY w_id
        HAVING publish >= 5
) AS counter;

Test: http://sqlfiddle.com/#!2/0e90f/30/0

于 2012-09-20T12:33:04.277 回答
2
select 
  w_name
from writers w
join articles a on w.w_id = a.w_id
group by w.w_id,w.w_name
having min(timestamp)>=5

用小提琴.. http://sqlfiddle.com/#!2/0e90f/11/0

或数..

select 
 count(w_name)
from writers w
join articles a on w.w_id = a.w_id
group by w.w_id,w.w_name
having min(timestamp)>=5

用小提琴.. http://sqlfiddle.com/#!2/0e90f/27/0

于 2012-09-20T12:31:09.903 回答
2

You need to have a subquery to get the writer's minimum timestamp.The result will then be counted sinceyou cannot aggregate a column that is already aggregated (eg. COUNT(MIN(columnName)))

SELECT COUNT(*) totalWriters
FROM
(
  SELECT  a.w_id, MIN(b.`timestamp`)
  FROM    writers a
          INNER JOIN articles b
            ON a.w_id = b.w_id
  GROUP BY a.w_id
  HAVING MIN(b.`timestamp`) >= 5
) a

SQLFiddle Demo

于 2012-09-20T12:32:58.443 回答
2

It's easy, here is the query:

SELECT count(DISTINCT w_id)
FROM articles
WHERE TIMESTAMP>=5 
AND w_id NOT IN (SELECT w_id FROM articles WHERE TIMESTAMP<5);

See SQL Fiddle

于 2012-09-20T13:11:52.593 回答
1

An easy solution:

SELECT COUNT(DISTINCT w_id)
FROM writers
WHERE w_id NOT IN
(SELECT w_id from articles
 WHERE timestamp <=5)
于 2012-09-20T12:33:32.793 回答
1
select count(*)
from (
    select w_id, min(`timestamp`) as `timestamp`
    from articles
    group by w_id
) s
where `timestamp` >= 5
于 2012-09-20T12:36:44.677 回答
1
select count(1) from (
     select w.w_id, count(a.timestamp) cnt from writers w
     inner join articles a on a.w_id = w.w_id
     group by w.w_id
  ) t
inner join articles art on art.w_id = t.w_id
where t.cnt = 1 and art.timestamp > 5
于 2012-09-20T12:45:39.270 回答
1

Try this, i have just tested..

select*   from ( select tblw.w_id, count(tblart.timestamp) count from writers tblw  inner join articles tblart on tblart.w_id = tblw.w_id   group by tblw.w_id ) temp inner join articles art on art.w_id = temp .w_id where temp .count = 1 and art.timestamp > 5
于 2012-09-20T12:56:07.130 回答