I am looking to implement this code into a site but I have a few questions. What does the code mean exactly? How does it translate? "end as x" what is it doing here? does anyone have a recommendation as to how to best use it with php? I found this script on PHP and asked the creator but I want to ask the community to see if I get a quicker response.
To better clarify, I am trying to select every hundred rows. I am trying to create a next button that will pull every 100 rows with every click of the next button or arrow. Like a pagination but without the number of the pages.
SELECT * FROM storeCoins WHERE intId in (SELECT CASE(intId%100=0) WHEN 1 THEN intId else 0 END AS X FROM storeCoins ORDER BY 'year' ASC
I don't understand what the syntax means but would really like to understand this. Seems very efficient. If you can recommend how to best implement this using PHP that would be very helpful. I created a next button that only shows the next id in the database but not the next 100. My biggest interest is in understanding this sql code.
it can be as simple as...
For page 1 use
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 0, 100
for page 2 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 99, 100
for page 3 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 199, 100
and so on
Related
I'm building an infinite scroll for a photo gallery of a website.
It starts with 6 photos loaded and should load 3 more photos every-time the user reach the end of the page.
So, me first mysql is:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 0,6
When the user reach the end of the page, I'm using the following mysql command to add 3 more photos:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,9
The problem is that it returns 4 records instead of 3 and I have no idea why it happens!
Somebody can help me with that? what am I doing wrong?!
To add three more photos, the second limit would be:
limit 6, 3
The arguments are offset and number of records. You are asking for 9 records starting on the 7th (the offset starts counting from zero).
You should learn more about MySQL.
LIMIT [start, ] count
So, in your second case it will be
LIMIT 6, 3
Here you can learn more about LIMIT: doc
Just write
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,3
2nd digit of LIMIT points quantity but not top limit
Now I've read on this fantabulous site about how to check if the timestamp that is in your database is before now(), and that answers part of my question.
But the remainder of my question still eludes me:
How can you not only check for timestamps BEFORE now(), but also put a cap on how many before now are queried?
I have a database that is acting as a guestbook, and the output is a flash piece that looks like a post-it board. you fill out a form, and the submission immediately "tacks" itself to the post-it board. The problem is that when you first load the page, it will load every single post starting from id-0 to id-5,000,000 and so on and so forth.
I would like to put a cap on how many are loaded so the query looks at the following things:
What's the current timestamp?
Go back in time (for example) 10 entries ago
Post from THAT point on
The fields I have in my database are: id, comments, timestamp
EDIT
I'm looking at some of the answers, and I would like to ask about the LIMIT. If I put a limit on the queries, will I still be able to query posts that are PAST now?
Example: Say there are two people viewing the site at the same time. One visitor posts a comment into the database. I want person 2 to still be able to the see that comment pop up on his end.
The flash post-it area runs off a php script that queries the database and exports it into an XML format that Flash can read. Every 5 seconds, the flash re-queries the database to check for more posts.
If I put the Limit on the query, will I still be able to grab NEW entries on the fly? I just want the LIMIT to generate a starting point offset from ground zero
I think what you are looking for is called Limit
You just put it at the end of your statement and the query will return the amount of results you wanted
YOUR QUERY LIMIT 0,10
This will return 10 first results
SELECT * FROM
(SELECT ... WHERE dt<NOW() ORDER BY dt DESC LIMIT 10) a
ORDER BY a.dt ASC
or
SELECT ... WHERE dt<NOW() ORDER BY dt DESC LIMIT 10
check which is the more suitable for you.
I have created a real estate website and I wanted to have the listings sorted by the last update and completeness of the listing. So I have been trying to figure out how to sort by a field in mysql (completion_score) in combination with the most recently updated listing. The completion score would be on a 100 point scale with 0 being bad and 100 being perfectly complete. I will have the completion score calculated when the listing is added and updated and saved in the mysql database. I am guessing that I will have to somehow combine the date and the completion_score to make a total, but I am unsure how to do this in one SELECT.
Currently I am using this (which obviously doesn't account for completion score):
ORDER BY ".$wpdb->prefix."fsrep_listings.listing_featured DESC, ".$wpdb->prefix."fsrep_listings.listing_last_updated DESC
I was thinking that if it was completed in the last week I would add 30 points to the completion score, if it was completed in the last month I would add 20 points to the completion score, and if it was completed in the last three months I would add 10 points to the completion score. I could then order it by this updated completion score. The problem is how do I have it change each day to adjust the score.
Taking Mark's advice I put in the following code:
ORDER BY $wpdb->prefix.'fsrep_listings.listing_score' + if($wpdb->prefix.'fsrep_listings.listing_last_updated' > NOW()-INTERVAL 1 WEEK,30,if($wpdb->prefix.'fsrep_listings.listing_last_updated' > NOW()-INTERVAL 1 MONTH,20,if($wpdb->prefix.'fsrep_listings.listing_last_updated' > NOW()-INTERVAL 3 MONTHS,10,0)))' DESC';
I then received the following Parse error: syntax error, unexpected T_IF in /home/...
Your idea sounds good. Why not run with it?
ORDER BY completion_score + IF(date > NOW()-INTERVAL 1 WEEK,30,IF(date > NOW()-INTERVAL 1 MONTH,20,IF(date > NOW()-INTERVAL 3 MONTHS,10,0))) DESC
Something to that effect anyway.
how about something like:
order by completeion_score - (to_days(now()) - to_days(completion_date))/3
or some similar function of the number of days that have passed since completion.
UPDATE
The code I've given you above is SQL only, there is no php involved, therefore everything should be within quotes except for the $wpdb->prefix parts. (This goes for #Mark's answer as well as most of the others that give you code, I'd imagine.)
to add your prefixes, your code should look something like this:
"ORDER BY ".$wpdb->prefix."fsrep_listings.listing_score - (to_days(now()) - to_days(".$wpdb->prefix."fsrep_listings.listing_last_updated))/3"
You need to decide exactly how you want the sorting to work, but if you want you can do ORDER BY completion_date DESC, completion_score DESC, which will give you the most recent days first, within equal dates they'll be ordered by score.
You can think it in reverse way, so that you don't have to update your old entries. Consider the current week 0, then next week 10, next 20 and so on. I think you don't need the QUERY for this :)
I am trying to implement the pagination in php. I am using the Mysql as back end database. I am trying to implement the pagination logic.
I would be having lots of record. But the user will see only 10 at a time.
Now to show the first page, i do a
SELECT * from USERS LIMIT 10.
Now to get the next 10 and the subsequent 10 records i am not able to write a query. Please help me fetch the in between records to support pagination logic. Also provide if any other suggestions for pagination.
You should use the OFFSET option.
SELECT * FROM Users LIMIT 10 OFFSET 10 (or 20, or 30);
That way you just pass the start position in the request when you hit next (or the page number) and you'll retrieve the records you want.
MySQL's limit feature can take two arguments:
select * from USERS limit 10,10
The above would retrieve 10 rows starting at row 10. Bear in mind that the MySQL row offset is 0 based, not 1. The first argument is the starting row, the second is the page size.
Also, if your page size is consistent, all you need to do is pass in the current page (default to zero). That would then allow you to specify the start row as a page * size.
I mean what the most efficient way to get information about the quantity of your page's items and make sql query with LIMIT that you need. or I should get all items and then crop array with php functions?
now I do 2 queries: first to count all items and second to get items that I need with LIMIT.
OK, I'll be more concrete. For example I need to show a question on my page and 20 answers to this question. At the bottom there shold be page control: links to the next, prev page and so on. I want to show proper number of links (number of answers/20) and when I go to any link I want to recieve proper answers (for example 41 to 60 on the 3d page). So what's the best way to get number of items (answers) to show proper number of links and to get proper answers for each link?
I guess your'e trying to say you want to know how many items/answers there is in the query but only read up to 20 items at at time, for pagination.
Firstly: You really should look for a pagination package; lots and lots of people have had the same problem before and there probably exists both free/opensource and proprietary solutions for your programming language and framework. (If you say what language you are using I'm sure someone can reccomend a solution for you.)
Anyway, I know I like to know how things work, so this is how it usually does:
As far as I know the pagination code calculates the pages by doing one query using select count(*) from tblX where something divide this number with the items-per-page number and use ceiling (e.g. 4.1 => 5).
For listing the results per page a new query is required; don't worry the count query is terribly much faster than getting every result discarding the ones you don't need DO NOT DO THAT (that's the recipie for becoming the top story on this page). Something like select * from tblX where something limit Y offset Z where Y is the number of items per page, and Z is the the (requested_page - 1)*Y; page 1 will have offset 0, page 2 have offset 20 (if thats what Y are) etc..
But do not try to implement this manually, it's unneccesary, tedious and error prone, much better to use your time customizing a readymade solution.
I'm assuming you want a count of the number of rows you'll be reading so as to do some pagination or similar? I don't understand your need for the LIMIT in the context of your question. However, if you just want a count of how many rows have been found, use one of the following.
You select the count of all rows such as:
select count(*) as counted, name, address
from contact
Or found rows:
SELECT SQL_CALC_FOUND_ROWS, name, address
from contact
This may be mysql specific I'm not sure.
Update:
For pagination you would do something like the following - (Psuedocode)
$rows = array($result)
$num_rows = sql_calc_found_rows
$per_page = 20
$pages = ceil($num_rows / $per_page)
page
$rows_this_page = array()
$rows_this_page = get_values($rows, (min index)$page_number * $per_page - $per_page, (max index)$page_number * $per_page - 1)