How would i get the last 20 results submitted in a mysql db row.
I need the 20 most recent user submitted results.
Cheers.
If you don't have timestamp or autoincrement field on that table then you can't.
Otherwise : select * from table order by id desc limit 0,20
If you are using an auto_increment primary key, its as simple as:
SELECT * FROM that_table
ORDER BY auto_increment_primary_key DESC
LIMIT 0, 20
See the Query below, there is an function available in mysql syntax below
SELECT FROM ORDER BY DESC LIMIT 0,20
Related
I was wondering how to select next and previous rows from a mysql database with reference to my currently selected row.
I found this question on SO How to get next/previous record in MySQL? but in this case the select is done based on higher and lower id from the reference. I would like to use earlier or later timestamp instead of ids.
There is no reason for using subqueries.
Next:
SELECT * FROM `my_table`
WHERE `the_timestamp` > 123456
ORDER BY `the_timestamp` ASC
LIMIT 1
Prev:
SELECT * FROM `my_table`
WHERE `the_timestamp` < 123456
ORDER BY `the_timestamp` DESC
LIMIT 1
Based on the article you linked to, you can use:
SELECT *
FROM `my_table`
WHERE `the_timestamp` = (
SELECT MIN(`the_timestamp`)
FROM `my_table`
WHERE `the_timestamp` > 1373493634
);
It's formatted for readability. Replace the timestamp I used (1373493634) with the timestamp you want to find the next record after.
I have a row in my db table that is datetime. How can i write a query to only all rows except one (the most recent).
I would have used
ORDER BY col_name DESC LIMIT 1
if i was choosing only the most recent.. but i actually require all but the most recent.
Thanks
Just select all rows but the first:
ORDER BY col_name DESC LIMIT 1, 18446744073709551615
See 13.2.9. SELECT Syntax which explains the LIMIT clause.
Title says:
MySQL Query to choose all but the most recent datetime
If have duplicated dates you'll have to go for:
select * from t
where val != (select max(val) from t);
This is because if there are 2 max values then limit will only filter the first one and you'll get the other max value in the resultset.
mysql fetch previous or next record order by anyother field name and not by using order by id
select * from table where id > $id order by name asc limit 1
select * from table where id < $id order by name desc limit 1
I am able to get next and previous records but in this case how can i
upgrade next and previous records.
ID Links orderID
14 Google.com 1
15 Yahoo.com 2
20 gmail.com 3
25 facebook.com 4
What about if i use + and - button in front each link to upgrade and downgrade them and then rearrange the menus order by orderID ?
Well, if you really want to do it in a single query, you can use a subquery to find out the ID you need to update. The problem lies in the fact that MySQL cannot update the same table that you're trying to subquery, for obvious data integrity reasons. So you'll need to use some workarounds for that, such as creating a temporary table in a subquery.
UPDATE table AS t
SET [...]
WHERE t.`id` = (select * FROM (select `id` from table where `id` > $id order by `id` asc limit 1) AS sq)
There is absolutely no need to do the two selects.
You can do the following:
UPDATE table SET field='some value' WHERE id=$id+1
UPDATE table SET field='some value' WHERE id=$id-1
There you go :)
I need help to write a MySQL query that would do the following for me:
It would select the last row from a certain column. Let's say the table name is 'mysite_codes' and the column name is 'code', there are many rows in this column, but I want the query to take the last added one and get my back one result in PHP (LIMIT 1 thing).
Would anyone please help me with this?
MySQL tables have no inherent sorting. If you want "the last added one" then you'll need an AUTO_INCREMENTing column like id.
Then you can write.
SELECT `code` FROM `mysite_codes` ORDER BY `id` DESC LIMIT 1
to get just the row with the highest id value.
Try this:
select code
from mysite_codes
order by add_date desc
limit 1
Assuming you have an auto-incremementing id column called something like "auto_id_column":
SELECT code FROM mysite_codes ORDER BY auto_id_column DESC LIMIT 0, 1;
What SQL query would I use to display the newest entry?
Details:
id is the primary field. I have other fields but that are not related to when they were added.
ORDER BY SomeColumn DESC
LIMIT 1
or
use the MAX() function
Since you didn't give any details about your table it is hard to answer
SELECT * from yourTable ORDER BY `id` DESC LIMIT 1;
Another (better) way would be to have a "date_added" column (date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP) so you could order by that column descending instead. Dates are more reliable than ID-assignment.
not sure if this is what your looking for but I use mysql_insert_id() after inserting a new row
The auto incremented ID columns are not always the latest records inserted, I've remember really painful experience with this behavior. Conditions where specific, it was mysql 4.1.x at the time and there was almost 1 million records, where 1 out of 3 deleted everiday, and others re inserted in the next 24hours. It made a huge mess when I realize ordering them via ID was not ordering them by age....
Since then, I use a specific column for doing age related sorts, and populating these fields with date = NOW() at each row insert.
Of course it will work to found the latest record as you want, doing an ORDER BY date DESC LIMIT 0,1on your query
SELECT Primary_Key_Field FROM table ORDER BY Primary_Key_Field DESC LIMIT 1
Replace Primary_Key_Field and table obviously :)