Mysql delete order by - mysql

I have a table and I only display the latest 30 rows by order by ID.
I'm trying to delete any rows after the 30 newest rows by using this query below.
DELETE FROM table WHERE type = 'test' ORDER BY id DESC LIMIT 30, 60
I keep getting this error below
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 60' at line 1
What am I doing wrong?

Try this one,
DELETE FROM table
WHERE ID IN
(
SELECT ID
FROM
(
SELECT ID
FROM table
WHERE Type = 'TEST'
ORDER BY ID
LIMIT 30,60
) a
)

Second edit: While MySQL supports LIMIT in delete statements, it does not allow an OFFSET. This means that you cannot skip the first 30 rows.
Make a subselect on id (or any other primary key):
DELETE FROM table WHERE id IN (SELECT id FROM table WHERE type = 'test' ORDER BY id DESC LIMIT 30, 60)

This is not possible this way.
You could try it with a nested select statement, somewhat like this:
DELETE FROM table
WHERE type = 'test'
AND ID IN (SELECT id from table where type = 'test' order by id desc limit 30 )

Try like this
DELETE FROM table WHERE id in(SELECT id FROM table WHERE type = "test" order by id desc limit 30, 60)

I was unable to use the limit clause in the sub-query, so the solution I use, somewhat messy, is:-
select group_concat(id) into #idList from
(
select id from table order by id desc limit 0,30
) as saveIds;
delete from table where not find_in_set(id,#idList)
Alternatively,
select group_concat(id) into #idList from
(
select id from table order by id desc limit 30
) as saveIds;
delete from table where find_in_set(id,#idList)

Related

find second max in a table using mysql query

I have a database with one table called "user" having two fields:
"id" (type: INTEGER, PRIMARY KEY)
"name" (type: VARCHAR(32))
I want to Write a standard SQL query which retrieves the second highest value of "id" from the "user" table. The value returned should be represented using the column name "id".
I have tried this but it gives me all ids:
SELECT `user`.`id`
FROM `user`
ORDER BY `user`.`id` ASC
LIMIT 0 , 30
some example data in my table:
id name
----------------
1 john
2 david
3 mike
I want to get '2' but now i'm getting :
id
----
1
2
3
I can do it with help of PHP but I want to know the way with mysql (SQL).
thanks
SELECT id
FROM `user`
ORDER BY id DESC -- start with highest
LIMIT 1 -- show only 1 row
OFFSET 1 ; -- but skip the first (skip 1 row)
SELECT id FROM user ORDER BY id ASC LIMIT 1, 1
select max(id) from user where id < (select max(id) from user);
select max(id) from user where id not in (Select max(id) from user);
SELECT id
FROM `user`
ORDER BY id ASC
LIMIT 1, 1
Using limit you can set an offset and the number of records you like being returned.
How 'bout
Select max(id) from user
where id <> (select max(id) from user)
This may work:
select max(id)-1 from user;

MySQL INSERT on duplicate key UPDATE with SELECT

I have a query like the following
INSERT INTO connections (`c_id`,`in`,`out`,`ip`,`userID`)
VALUES (
(
SELECT c_id
FROM connections
WHERE (a bunch of conditions)
ORDER BY c_id DESC LIMIT 1
),
'1373799802',
0,
INET_ATON('127.0.0.1'),
4
)
ON DUPLICATE KEY UPDATE `out` = 1
Which throws the following error
1093 - You can't specify target table 'connections' for update in FROM clause
Obviously I can't have a SELECT clause within the insert into on duplicate update syntax, but I'd really rather do that instead of have 2 queries running. Can anyone tell me how I can do this?
Try like this instead:
INSERT INTO connections (`c_id`,`in`,`out`,`ip`,`userID`)
VALUES (
(
SELECT p.c_id
FROM (select * from connections) p
WHERE (a bunch of conditions)
ORDER BY p.c_id DESC LIMIT 1
),
'1373799802',
0,
INET_ATON('127.0.0.1'),
4
)
ON DUPLICATE KEY UPDATE `out` = 1
This issue seems due to a bug in mysql version 4.1.7 which states that
you can't update the same table which you use in the SELECT part
see Here
Not sure if this is the same version you are using as well.
try this
INSERT INTO connections (`c_id`,`in`,`out`,`ip`,`userID`)
SELECT c_id ,'1373799802', 0, INET_ATON('127.0.0.1'),4
FROM connections
WHERE (a bunch of conditions)
ORDER BY c_id DESC LIMIT 1
ON DUPLICATE KEY UPDATE `out` = 1
The following code inside your query:
SELECT c_id
FROM connections
WHERE (a bunch of conditions)
ORDER BY c_id DESC
LIMIT 1
actually results in a table and not a single value. For a successful attempt, try this:
INSERT INTO connections (`c_id`,`in`,`out`,`ip`,`userID`)
SELECT c_id,
'1373799802',
0,
INET_ATON('127.0.0.1'),
4
FROM connections
WHERE (a bunch of conditions)
ORDER BY c_id DESC LIMIT 1
ON DUPLICATE KEY
UPDATE `out` = 1

Using Union All and Order By in MySQL

I've 2 tables:
create table advertised_products(id int,title varchar(99),timestamp timestamp);
insert advertised_products select 1,'t1',curdate();
create table wanted_products(id int,title varchar(99),timestamp timestamp);
insert wanted_products select 1,'t1',now();
I'm using this query to get the records:
(
SELECT
ap.*,
'advertised' as type
FROM advertised_products as ap
)
union all
(
SELECT
wp.*,
'wanted' as type
FROM wanted_products as wp
)
ORDER BY timestamp desc limit 3
But it gives error:
Column 'timestamp' in order clause is ambiguous
How can i sort this?
Wrap it in a subquery.
SELECT s.*
FROM
(
SELECT ap.*, 'advertised' as type
FROM advertised_products as ap
union all
SELECT wp.*, 'wanted' as type
FROM wanted_products as wp
) s
ORDER BY s.timestamp desc
limit 3
Error lies in here,
"ORDER BY timestamp desc limit 3"
as you are combining the two tables query must know which fields are you using in which table.clearly that you are missing the table reference in your "orderby" clause
mention the table name/alias of the table name like below
"ORDER BY your_table_name.timestamp desc limit 3"

How to delete all rows except the 50 newest

how i can delete all rows in table recentposts
DELETE FROM recentposts WHERE recentposts.`userId` = 12 AND recentposts.`Id`
NOT IN (SELECT * FROM recentposts WHERE `userId` = 12 ORDER BY viewdate LIMIT 50)
i try many similar to this but they not worked. can someone tell me how i can do this in Mysql.
What about this?
DELETE FROM recentposts
WHERE
recentposts.`userId` = 12
AND
recentposts.`Id` NOT IN (SELECT Id
FROM recentposts
WHERE `userId` = 12
ORDER BY viewdate DESC LIMIT 50)
DELETE FROM `recentpost`
WHERE WHERE userId = 12 AND id NOT IN (
SELECT id
FROM (
SELECT id
FROM `table`
ORDER BY id DESC
LIMIT 50
) foo
);
PS: only working (MySQL 5.0.67 and upper) AS in earlier versions These are limitations of MySQL.
you can't DELETE and SELECT from a given table in the same query.
MySQL does not support LIMIT in a subquery.
so for previous version u can come up with is to do this in two stages:
first step
SELECT id FROM mytable ORDER BY id DESC LIMIT n;
Collect the id's and make them into a comma-separated string:
DELETE FROM mytable WHERE id NOT IN ( ...comma-separated string... );

MySQL reverse order without DESC

SELECT id FROM table LIMIT 8, 3
results in 8,9,10
but I need 10,9,8
How can you do this? If you add "ORDER BY id DESC" it gets 3,2,1
Put your query in a subselect and then reverse the order in the outer select:
SELECT id from (
SELECT id FROM table ORDER BY id LIMIT 8, 3
) AS T1 ORDER BY id DESC
Test data:
CREATE TABLE table1 (id INT NOT NULL);
INSERT INTO table1 (id) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11);
SELECT id from (
SELECT id FROM table1 ORDER BY id LIMIT 8, 3
) AS T1 ORDER BY id DESC
Result:
10
9
8
Note that the ORDER BY in the subquery is required otherwise the order is undefined. Thanks to Lasse for pointing this out!
First of all, if you're not ordering at all, that you got 8,9,10 right now might be related to the index used. Are you sure this isn't going to change in the future?
What I'm getting at is that unless you specify an order, you should not rely on it being the one you want.
So I would definitely add an order to that select to specify what you want. Otherwise you're only saying "give me 3 numbers from this table", not "give me 3 numbers from this table according to these rules". Why is 3,2,1 wrong but 8,9,10 right? You're not specifying.
Anyway, to answer your question, you must order after the limit, which means a subselect:
SELECT id FROM (
SELECT id FROM table LIMIT 8, 3
) AS dummy ORDER BY id DESC
However, I would try this SQL instead, related to the part about specifying:
SELECT id FROM (
SELECT id FROM table ORDER BY id LIMIT 8, 3
) AS dummy ORDER BY id DESC