mercredi 6 janvier 2016

How many rows selected?

I have a table like this:

// mytable
+----+-----------------+
| id |    timestamp    |
+----+-----------------+
| 1  | 1452001861      |    -- yesterday
| 2  | 1452088272      |    -- today
| 3  | 1452088283      |    -- today
| 4  | 1451915461      |    -- last week
| 5  | 1452001861      |    -- yesterday
| 6  | 1452088263      |    -- today
| 7  | 1252388263      |    -- out of {today, yesterday, last week}
| 8  | 1452088312      |    -- today
| 9  | 1452001762      |    -- yesterday
| 10 | 1222388263      |    -- out of {today, yesterday, last week}
| 12 | 1451915459      |    -- last week
+----+-----------------+

And here is my query:

SELECT * FROM mytable 
   WHERE DATE(FROM_UNIXTIME(timestamp)) >= CURDATE() - INTERVAL 7 DAY
   ORDER BY timestamp DESC

The above query select all rows with are between now and 7 last days.


Now sometimes all rows are before 7 last days (I mean is the newest row is for example added in 8 last days) and then the output of that query will be nothing (0 row selected).

What I want is, if there is no row selected (0 rows), then select 5 last rows (which were added earlier). How can I do that?

Aucun commentaire:

Enregistrer un commentaire