mankee mankee - 5 months ago 8
MySQL Question

SQL Query - Return all values up to particular value

I am trying to execute a SQL query which first sorts the data with the most recent one in row 1, and then returns all entries from row 1 up to an entry with a particular ID. The purpose of this is to load content to a mobile app, and when the user refreshes the content, only the content which is newer than the newest one he has already loaded is returned. So far I came up with this :

$all_feed_posts = mysqli_query($conn,"SELECT *
FROM feed_img_vid
WHERE city_geo_name_id = '$geo_name_id'
ORDER BY timestamp DESC
LIMIT WHERE feed_instance_id = $offset");


however I'm not sure if the limit keyword is the right one to use here.

Answer

If you have last id been read in $offset var then you can just add AND into your clause.

I guess all newer records should have feed_instance_id > $offset:

  SELECT * 
  FROM feed_img_vid 
  WHERE city_geo_name_id = '$geo_name_id' 
     AND feed_instance_id > $offset
  ORDER BY timestamp DESC 
Comments