I have a table with many fields, one of them is the date (format yyyy/mm/dd). I want to make a query that gets all entries from the days before e make it keep going down. I'm going to try to explain it better with examples.
My query:
SELECT
id,
date,
site,
url,
FROM links
WHERE online = "yes" AND
data <= ? AND
category != ? AND
ORDER BY date DESC, clicks DESC
LIMIT 200
The array:
Array
(
[0] => 2014/02/25
[1] => Adult
)
Problem is, I don't want to set a limit. Since the limit would be all the fields from the same date.
I can't set the field date = yyyy/mm/dd
because I'm using a script that has an infinite scroll style, so I don't know which date it's gonna be. Besides, I don't have data from all days, so it's gonna fail at some point.
UPDATE 2014/02/25 13:14:00:
I found the solution, don't know if it's the best one, but worked really well.
SELECT
id,
date,
site,
url
FROM links
WHERE publish = "yes" AND
date = ( SELECT date FROM links WHERE date < '2014/02/25' ORDER BY date DESC LIMIT 1 )
AND category!= 'Adult'
ORDER BY date DESC, clicks DESC
LIMIT 200
So, it's gonna get only the links from 2014/02/24, if it doesn't exists, then it's gonna fetch data from 2014/02/23 and so on.
Store into a variable the last date retrieved. And then use it for the next query. If the query result is empty, then you try again with the day before until you get a result.
You can better your script if you first query a list of "available" dates using a SELECT DISTINCT date for instance
is your date row in the Table of the type "date" ? You should convert the yyyy/mm/dd in yyyy-mm-dd, so you can write it in the table.
The Select could look like this:
SELECT ...
FROM tabelle
WHERE date BETWEEN '2007-09-20' AND '2007-09-23'
I found the solution, don't know if it's the best one, but worked really well.
SELECT
id,
date,
site,
url
FROM links
WHERE publish = "yes" AND
date = ( SELECT date FROM links WHERE date < '2014/02/25' ORDER BY date DESC LIMIT 1 )
AND category!= 'Adult'
ORDER BY date DESC, clicks DESC
LIMIT 200
So, it's gonna get only the links from 2014/02/24, if it doesn't exists, then it's gonna fetch data from 2014/02/23 and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.