I want to retrain last 25 entered records and delete remaining records according to id.
DELETE * FROM list
WHERE id NOT IN
(
SELECT *
FROM (
SELECT *
FROM 'list'
ORDER BY id DESC LIMIT 25
) as rows
)
DELETE *
FROM 'list'
WHERE id NOT IN ( SELECT id
FROM 'list'
ORDER BY id DESC
LIMIT 25 )
Deleting while selecting from the same table isn't permitted in MySQL.
You can try something like this:
SELECT #rows_to_delete:=COUNT(*)-25 FROM list;
DELETE FROM list ORDER BY id ASC LIMIT 0, #rows_to_delete;
NB: this is not tested please test before running it on real data.
I think your query is close, but what you need to change is just to filter for id in your subquery, don't select everything, because that doesn't make sense. You want to see if a single item (id) is not in a group of things (all columns).
Try changing your query to this:
DELETE FROM list
WHERE id NOT IN(
SELECT id
FROM list
ORDER BY id DESC
LIMIT 25);
I would test this with some dummy data first, but I believe it will do what you want.
Related
I have issue there with select from table. I want to select all rows except for first row. So .. There is my code
SELECT * FROM table ORDER BY id DESC
So this code select and order id's from table which give me id feedback "5>4>3>2>1". And there is issue .. How I can select and echo just 4>3>2>1 rows.
So if I had rows with id's 1,2,6,8,10 , echo will be 10,8,6,2,1 and I want select to echo just 8,6,2,1.
There is my full wrong code for select.
$other = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 1, 1");
This should do it.
SELECT * FROM table WHERE id NOT IN (SELECT MAX(id) FROM table) ORDER BY id DESC
Try this:
SELECT *
FROM
(
SELECT *, row_number()
OVER (ORDER BY id DESC) row
FROM table
)
WHERE row != 1
It gives numbers to your selected rows and takes all of them without the one with row number 1
Try this
$other = mysql_query("SELECT * FROM table ORDER BY id DESC OFFSET 1");
THE ABOVE QUERY WONT WORK AS A LIMIT IS NEEDED
Refer to this answer
All you need is offset 1, but offset cannot be used without limit. So, I'd suggest something like:
SELECT * FROM table ORDER BY id DESC LIMIT 99999999 OFFSET 1
Warning: make sure your table doesn't contain lots of records, otherwise you will run into performance issues. Or, change the limit to something reasonable, like 10.
EDIT:
Read: How to use offset without limit
SELECT *
FROM table
WHERE id NOT IN ( SELECT id
FROM table
ORDER BY id DESC
LIMIT 1 )
ORDER BY id DESC;
You can try this.
In this case I am selecting all the rows, except the one with the biggest id.
In the example of [1,2,3,4,5], it will be :
SELECT *
FROM table
WHERE id NOT IN ( 5 )
ORDER BY id DESC;
Hope this helps!
Can anyone please help to get last record from the group.enter image description here
I think you need this:
select * from t where col = 85 order by id desc limit 1
According to your comment, this should get last records for every group: (this assumes that id is unique and "last record" means record, with highest id)
select t.* from t
inner join (select max(id) as maxid from t group by col) s
on t.id = s.maxid
To fetch the 1 row from mysql use 'limit' keyword.
MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
E.g.:
From your screenshot, subscription id is same for multiple id's you want to get last record which id is greater. The below query gets your result, grouped by subscription_id and ordered by id desc and limiting to 1 makes fetching only 1 row from database.
select * from tableName group by subscription_id order by id desc limit 1
You can used last() function.
SELECT LAST(CustomerName) AS LastCustomer FROM Customers;
I want to run a query where the last 5 entries are always returned, which is simple: SELECT * FROM table ORDER BY id DESC LIMIT 5. But I want to get more than 5 if a certain condition applies, which by itself would simply be something like SELECT * FROM table WHERE field > value. Is there a way to do this as a single query or do I have to run 2?
Maybe with UNION?. Like:
(SELECT * FROM table ORDER BY id DESC LIMIT 5)
UNION
(SELECT * FROM table WHERE field > value)
I try to program a solution where I have multiple linked tables.
Now I have another problem:
I want to limit count of returned lines to 1000.
But I want to show ID 1-1000, next page 1001-2000.
IDs may be stored in iregular order in database (ID 1 does not have to be the first row)
Not any problem so far:
SELECT * FROM table ORDER BY id ASC LIMIT 1000
BUT
Now I have to sort resulty by another column:
SELECT * FROM table ORDER BY name ASC LIMIT 1000
which might return other IDs than 1-1000
or I do
SELECT * FROM table ORDER BY id ASC, ORDER BY name ASC LIMIT 1000
But this will only sort by ID an then by name. So if I would have any ID as duplicate (which is not possible) I would then have those sorted by name.
How can I achive that I get the first 1000 IDs (some IDs might not exist as they might have been deleted before!) and those thousand rows sorted by name?
Is that even possible?
Try this:
SELECT * FROM table WHERE id IN (
SELECT ID FROM table ORDER BY ID ASC LIMIT 0, 1000
) ORDER BY name ASC
As mentioned in comments that subquery is not supported, 'This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'. Using JOINs is the only alternative left, like this:
SELECT table.* FROM tab JOIN (
SELECT ID FROM table ORDER BY ID LIMIT 1000
) temp
ON table.ID = temp.ID
ORDER BY table.name
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query