I want remove duplicate record & use this query:
DELETE FROM news e
GROUP BY e.itemId, e.tag_id
HAVING COUNT(e.itemId) > 1
AND COUNT(e.tag_id) > 1
but get this error:
[Err] 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 'e
How can I do this?
I'm not sure what you're trying to acheive here, please expand your explanation to add a little more info. From what i see you actually need to create a sub query as you can't use GROUP BY directly on the delete, try something like this:
delete from table
where columnA in (
select columnA
from (
select columnA
from YourTable
group by columnA
having count(*) > 1
) t
)
Not exactly tailored to your problem but you should get the idea.
for remove duplicate records, use this query
DELETE
n1
FROM
news n1,
news n2
WHERE
n1.id < n2.id
AND n1.itemId = n2.itemId
AND n1.tag_id = n2.tag_id
AND n1.tag_id IS NOT NULL
Related
Trying to delete a specific amount of rows in a MySQL query, I am able to SELECT whatever I want to delete with the following command, getting the results I need:
select * from ns_cos ns where ns.created_at <>
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
However, when I try to delete the selected data with:
delete from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
I get:
SQL Error [1064] [42000]: (conn=5159) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ns where ns.created_at not exists (select max(nsa.created_at) from ns_cos nsa wh' at line 1
What am I doing wrong?
Your immediate issue is that not all MySQL versions support aliasing the table directly in delete from. Furthermore, though, you cannot re-open the table you delete from in the from clause.
Consider using the delete ... join syntax.
delete ns
from ns_cos ns
inner join (
select month_year, max(nsa.created_at) created_at
from ns_cos nsa
group by month_year
) ns1 on ns1.month_year = ns.month_year and ns1.created_at <> ns.created_at
EXISTS in there not posible use IN clause, but you need to enclose the table in a seprate select, so that mysql thinks it is another table
delete from ns_cos ns
where ns.created_at not IN (select max(nsa.created_at) from (SELECT * FROM ns_cos) nsa where nsa.month_year = ns.month_year)
This happens when you join tables from two schemas, no matter whether you take aliases for the table names or not.
Error
SQL Error [1064] [42000]: 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 '...'
delete !ALIAS! from table ALIAS ...
Strangely, only with aliases, you can get around this error in a one-step SQL. Tested in MySQL 5.7.
Try this pattern, using the alias of the table you want to delete from between delete and from:
delete t1 from table t1
join table2 t2
on t1.id = t2.id
Your code would be:
delete ns from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
Other steps I checked before finding out the alias trick (do not read)
I tried it with a view of the other schema's table in the same schema, that is not enough.
One way to get around this is to make a copy of the table in the same schema (and delete that copy afterwards).
You might also somehow make a full "linked server link" like in T-SQL [linkedservername].DB1.Schema.Table1 at How to join two tables if they are in different schemas Your code is on the same server, but this might still help jumping over to the other schema, untested.
So basically, I am trying to select a row from my database, based on two values, I have tried doing
SELECT FROM users WHERE first = 'Bob' AND last = 'Stevenson';
but when I do it it gives this error:
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 'FROM users WHERE first = 'Robert' AND last = 'Westbury'' at line 1
Thanks in advance,
Robert
You need to specify what column(s) do you want to select, SELECT statement should be followed by the column names or by * to select all the columns:
SELECT * FROM users WHERE first = 'Bob' AND last = 'Stevenson'
Dude, it's Select (* OR <column_name_1>, <column_name_2>... ) From <table_name> where <condition>;
When I attempt to run the following query in phpmyadmin or directly in the cli,
DELETE FROM mdl_enrol n1 WHERE n1.id > (
SELECT n2.id from mdl_enrol n2
WHERE n2.enrol = "database"
AND n1.id > n2.id
AND n2.courseid = n1.courseid
)
AND n1.enrol = "database"
I get the following error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'n1 WHERE n1.id > (SELECT n2.id from mdl_enrol n2 WHERE n2.enrol = "da' at line 1
If I run the command as a SELECT * it works fine and returns the right number of rows.
Why does this keep cutting off partway through my string "database"?
As mentioned in comments, MySQL doesn't return the whole query in error message. And you don't need it as you already know which query you are submitting (if it was done e.g. via PHP, you could "echo" the query before executing). MySQL always returns the query from the first character which caused the error.
In your case, the error is in ... mdl_enroln1.... According to MySQL manual, in DELETEsyntax, you are not allowed to specify table aliases as in SELECT queries. So you have to rewrite your query without the n1 alias and use only the full table name mdl_enrol.
Ahh, thanks for pointing out my noob mistake all :) I was having some other issues with PHPmyadmin cutting off my saved queries so I thought the two were related somehow and it was frustrating me!
Anyway, I found a solution to my query, working off of Marki555's answer. I had to modify my query as so:
DELETE n1 FROM mdl_enrol AS n1 WHERE n1.id > (
SELECT n2.id FROM (
SELECT * FROM mdl_enrol
) AS n2
WHERE n2.enrol = "database"
AND n1.id > n2.id
AND n2.courseid = n1.courseid
)
AND n1.enrol = "database"
Yes, the DELETE syntax does not directly allow you to use aliases, but you can use the AS syntax.
Then I ran into the issue of not being allowed to define an alias within the WHERE clause (for n2) so I had to create a separate FROM clause within the WHERE clause that defines mdl_enrol (using SELECT * FROM mdl_enrol) as n2
This was taken from this guide
Thanks for the help!
I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
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 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1
I have such sql-query:
DELETE `LINK_LA_TYP`
FROM
`LINK_LA_TYP`
JOIN `LINK_ART` ON `LINK_LA_TYP`.LAT_LA_ID = `LINK_ART`.LA_ID
JOIN `ARTICLES` ON `LINK_ART`.LA_ART_ID = `ARTICLES`.ART_ID
WHERE
(
`ARTICLES`.ART_SUP_ID IN(
10008,
10439,
11005,
...
...
441
)
)
LIMIT 50000;
But i get error.... From mysql-doc i get that with delete+join+limit i will get errors....
But how can i change my code? (new to mysql and sql in all). How to change my code? To limit rows to delete....
Also in phpmyadmin i get
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 'limit 50' at line 1
From the docs For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used. dev.mysql.com/doc/refman/5.0/en/delete.html – Michael Berkowski 2 mins ago
I concur with this. Additionally,
The DELETE command cannot have a LIMIT clause. Also, generally speaking you cannot DELETE from multiple JOINed tables.
It MIGHT be possible to rewrite the statement using a subselect something like:
delete from LINK_LA_TYP
where LAT_LA_ID in
(select LA_ID
from LINK_ART
join ARTICLES on ARTICLES.ART_ID = LINK_ART.LA_ART_ID
where ARTICLES.ART_SUP_ID in (...));