How to write a MySQL query to achieve this task?
Table: writers
w_id w_name
---------------
1 Michael
2 Samantha
3 John
---------------
Table: articles
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
----------------------------------------
Need to COUNT only those writers who published their first article not earlier than 5.
(only writers who published at least one article can be counted)
In this example the result would be: 1 (one writer – Samantha)
SQL code can be tested in this SQLFiddle
You need a double SELECT.
First you fetch
w_id, together with timestamp of first article:Then you request that
publishis not earlier than 5:Then you join that “table” with
writersto get the name, if you want.But if you only want the count, you do not need the
writerstable at all:Test: http://sqlfiddle.com/#!2/0e90f/30/0