MySQL只选择新记录

时间:2012-09-18 21:03:08

标签: mysql sql select

如何编写MySQL查询来实现此任务?

表:作家

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

表:文章

a_id   w_id   timestamp   a_name
----------------------------------------
  1      1    0000000001  PHP programming
  2      3    0000000003  Other programming languages
  3      3    0000000005  Another article
  4      2    0000000015  Web design
  5      1    0000000020  MySQL
----------------------------------------

只需要选择那些不早于 0000000005 发表第一篇文章的作者。((只能选择发表至少一篇文章的作者)

在此示例中,结果为:

  2     Samantha

可以在此处测试SQL代码http://sqlfiddle.com/#!2/7a308

1 个答案:

答案 0 :(得分:3)

未经测试,但很接近:

SELECT w_id, MIN(timestamp) as min_time
from writers w
JOIN articles a on w.w_id = a.w_id
GROUP BY 1
HAVING min_time > 5