remove duplicate records syntax error in SQL query - mysql

i try to remove duplicate records from table
my sql query is :
DELETE FROM products_description AS t1 WHERE EXISTS (
SELECT 'products_id', 'site_language_id'
FROM products_description AS t2
WHERE t2.products_id = t1.products_id
AND t2.site_language_id = t1.site_language_id
);
it give this error :
#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 'as t1
where exists (select 'products_id','site_language_id' from products_desc' at line 1
i don't know what is missing ?

To delete all records which have duplicates
DELETE FROM products_description
WHERE product_id IN (
SELECT product_id FROM( Select product_id,site_language_id
From products_description
Group By product_id,site_language_id
Having Count(*)>1
) As X
)As Y

MySQL is complaining since there is a syntax error. Try replacing the quote (') with a backtick (`).
Be careful though! The query will then work but will delete all rows rather than just duplicates....

Related

DELETE FROM throws a SQL Error [1064] [42000]: (conn=5159) on a query, on which a SELECT works

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.

SQL execution error

I try this query
INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod
However I got this error
SQL execution error # 1065.Response from the database:
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') SELECT product.id_prod,product_lang.name,product.ref from' at line 1
Two problems:
DESC is a reserved keyword. Use backquote (``) for desc.
Change prod_lang to product_lang in the query.
Solution:
INSERT INTO shop.product (prod_id,model,`desc`)
SELECT product.id_prod,product_lang.name,product.ref
from product left join
product_lang on product_lang.id_prod = product.id_prod
Note:
It is a good practice to use backquotes for all columns eventhough it is not a reserved keyword.

#1064 Syntax Error : In "Update Query"

When I use this mysql code
update catalog_product_entity_decimal
set value = "6.0000"
where attribute_id = 75 and
entity_in ( SELECT
product_id
from `catalog_category_product`
WHERE category_id =37 );
i'm getting this error
#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 'SELECT product_id from `catalog_category_product` ' at line 4
I couldn't figure out what's wrong with my query. Can someone tell me what's wrong?
Thank you
You are missing the IN clause after entity_in. Use this:
update catalog_product_entity_decimal
set value = "6.0000"
where attribute_id = 75 and
entity_id IN ( SELECT
product_id
from `catalog_category_product`
WHERE category_id =37 );
I think catalog_category_product will not contain the ' symbol.
Try the query without using the ' symbol
You missed = OR in in the query

Using COUNT and DISTINCT keywords together to count distinct values

I am writing the query to count the number of distinct values in the database table. So I wrote the query as:
select count ( distinct user_id ) as unique_users from 1_time_access;
I am getting error
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 'distinct user_id ) as unique_users from 1_time_access' at line 1
Can anyone explain what am I missing?
Remove the space between count and the open parenthesis.
select count(distinct user_id) as unique_users from 1_time_access;

Mysql join 2 database and 3 tables query?

SELECT db1_t1.userid as userid
, db1_t1.customer_id as vw_customer
, db2_t1.customers_id as customer
, db2_t1.orders_id as order
FROM database1.table1 db1_t1
LEFT JOIN database2.table1 db2_t1
ON db1_t1.customer_id = db2_t1.customers_id
It gives me 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 'order FROM
database1.table1 db1_t1 LEFT JOIN
database2.' at line 2
I am using php and mysql.
order is a keyword - think ORDER BY my_column.
I'd suggest renaming it, but you could enclose it in backticks
db2_t1.orders_id AS `order`