MySQL Delete older records - mysql

I have thousands of records (included duplicate posts) so now I want to delete old records (just leave the latest record) based on date.
My code is given below
DELETE a.*
FROM dle_post AS a
INNER JOIN (
SELECT title, MIN( id ) AS min_id
FROM dle_post
GROUP BY title
HAVING COUNT( * ) > 1
) AS b ON b.title = a.title
AND b.min_id <> a.id
The problem is that it random records base on ID. I really appreciate your help!

If you want to base it on date, you should use MAX(date) in the subquery.
DELETE a.*
FROM dle_post AS a
INNER JOIN (
SELECT title, MAX(date) AS maxdate
FROM dle_post
GROUP BY title
HAVING COUNT( * ) > 1
) AS b
ON b.title = a.title
AND a.date < b.maxdate

Just create the SELECT query of posts you want to delete and put and in a subselect:
DELETE FROM dle_post WHERE id IN (SELECT id FROM dle_post WHERE ... )
This is more readable and maintainable.

Related

Referring to CTE in WHERE condition of UPDATE

I'm trying to run a query like this:
WITH cte AS
(
SELECT id, category, SUM(amount) AS sum_amount FROM t1 GROUP BY id, category
)
UPDATE table SET amount = cte.sum_amount WHERE id = cte.id;
However, I keep getting the error
Unknown column 'cte.id in WHERE clause'
Does anyone know how I can refer to my common table expression in the UPDATE query, or otherwise, rewrite it?
You can try below
WITH cte AS
(
SELECT id, SUM(amount) AS sum_amount FROM t1 GROUP BY category
)
UPDATE T
SET T.sum_amount= CT.sum_amount
FROM table T
JOIN cte CT
ON T.id = CT.id
Alternate way with Temporary table, you can read about CTE and Temporary Table
Temporary table:
SELECT id, category, SUM(amount) AS sum_amount
INTO #temp
FROM t1 GROUP BY id, category
Update query with temp table:
UPDATE OT
SET OT.sum_amount= TT.sum_amount
FROM table OT
JOIN #temp TT
ON OT.id = TT.id
CTE:
WITH cte AS
(
SELECT id, category, SUM(amount) AS sum_amount FROM t1 GROUP BY id,category
)
UPDATE T
SET T.sum_amount= CT.sum_amount
FROM table T
JOIN cte CT
ON T.id = CT.id

Why do I receive an error at this join tables?

I wrote this:
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN (SELECT *
FROM T
WHERE T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC
LIMIT 10)
and I receive this
"Unknown column 'T1.CATEGORY' in 'where clause'".
Why?
Update:
My purpose of this is to get 10 posts of any category.
Because T1 is not visible from within the subquery.
Your JOIN also serves no purpose and/or you probably forgot the JOIN condition.
In JOIN condition should use ON keyword
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN SELECT * FROM T ON T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC LIMIT 10;
If you need to get 10 posts of each category you can use a query like this:
SELECT CATEGORY, Post
FROM (
SELECT a.CATEGORY, a.Post, count(*) as rn
FROM #T a
JOIN #T b ON a.CATEGORY = b.CATEGORY AND a.Post >= b.Post
GROUP BY a.CATEGORY, a.Post) dt
WHERE rn < 11;

remove duplicates in a column

I have the following query to basically find all duplicates in my username column:
SELECT `username`
FROM `instagram_user`
GROUP BY `username`
HAVING COUNT( * ) >1
How do I remove all the duplicates, such that it will only leave me with one unique username in the table? I don't care which entity it is that is persisted or removed, as long as there's one unique username in the table.
If you don't care what record to choose then just add a unique constraint while using IGNORE
ALTER IGNORE TABLE instagram_user ADD UNIQUE (username);
Here is SQLFiddle demo
and MySQL will do the job for you. You want to have that unique constraint anyway in order to keep your table out of duplicates in the future.
or alternatively you can do
DELETE t
FROM instagram_user t JOIN
(
SELECT username, MAX(id) id
FROM instagram_user
GROUP BY username
HAVING COUNT(*) > 1
) q
ON t.username = q.username
AND t.id <> q.id
This one will leave only a row with max id for rows that have duplicate usernames.
Here is SQLFiddle demo
Not sure this is for SQL server, you can try a similar code in mysql.
;With CteUsers AS(
SELECT *,ROW_NUMBER() OVER (PARTITION BY username Order by username) AS ROWID
FROM(
SELECT PkId, `username`
FROM `instagram_user`
)tbltemp)
SELECT * FROM CteUsers;
This will result as follow
PkId username RowId
1 xx 1
2 xx 2
....
then delete where RowId > 1
;With CteUsers AS(
SELECT *,ROW_NUMBER() OVER (PARTITION BY username Order by username) AS ROWID
FROM(
SELECT PkId, `username`
FROM `instagram_user`
)tbltemp)
DELETE instagram_user WHERE PkId iN (SELECT PkId FROM CteUsers WHERE ROWID > 1);
This will give you the duplicates (i.e. the ones you need to delete) ...
select a.id, a.username from instagram_user a, instagram_user b
where a.username = b.username and a.id <> b.id
and b.id = (select min(id) from instagram_user where username = a.username)
so the DELETE would be something like ...
delete from instagram_user where id in
(select a.id from instagram_user a, instagram_user b
where a.username = b.username and a.id <> b.id
and b.id = (select min(c.id) from instagram_user c
where c.username = a.username))

Select 5th highest Value without using LIMIT

I am new to SQL and I want to fetch 5th highest value without using LIMIT. Here is the code I am trying but it is not working properly. It is showing the 5th lowest value instead of 5th highest value.
SELECT a . * FROM user AS a
WHERE 5 =
(SELECT count( DISTINCT b.id ) FROM user AS b WHERE b.id >= a.id ORDER BY a.id DESC)
Can anyone help me with this?
You could also do:
SET #nth := 5;
SELECT
a.*
FROM jos_modules AS a
WHERE #nth = (
SELECT
COUNT(b.id)
FROM user AS b
WHERE
a.id >= b.id
);
Try this
SELECT a . *
FROM user AS a
WHERE 5 = (
SELECT count( DISTINCT b.id )
FROM user AS b
WHERE a.id >= b.id ORDER BY a.id )
select * from (
select a.* , row_number() over (order by id asc) as RANK
from a ) where RANK=5 ;
If you are using a Teradata DB , you can use qualify statement :
select * from a
qualify row_number () over(order by id asc)=5;

SQL select the last 3 dates from a table

I have a table with lots of fields in mysql
I need a query to return (in the same raw!) the top last 3 dates (dates can have large gaps between them)
ie:
2012/01/20
2012/01/18
2012/01/12
2012/01/10
2012/01/04
etc...
Any help will be appreciated
I must get them in the same row!
This is the query I am trying to use with no success:
SELECT a.id, a.thedate, b.id AS id1, b.thedate AS thedate1, c.id AS id2, c.thedate as thedate2
FROM mytable AS a INNER JOIN mytable AS b ON a.id = b.id INNER JOIN mytable AS c ON b.id=c.id
WHERE c.thedate = SELECT MAX(thedate)
EDIT :
SELECT group_concat(date) FROM (SELECT date FROM my_table ORDER BY date DESC LIMIT 3) AS temp
Corrected-
SELECT group_concat(date) FROM ( select date from table_name order by date desc limit 3) as a
SELECT GROUP_CONCAT(a.date )
FROM (
SELECT date
FROM my_table
ORDER BY date DESC
LIMIT 3
) AS a