Why the SQL shows MySQL Error? - mysql

Why this sql shows mysql error?
DELETE FROM wp_comments
JOIN wp_commentmeta ON wp_comments.comment_id =wp_commentmeta.comment_id
AND wp_commentmeta.meta_key = 'somekey'
AND wp_comments.comment_post_ID = '1'
error string
[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 'INNER JOIN wp_commentmeta ON
wp_comments.comment_id =wp_commentmeta.comment_id A' at line 2
Please Advice me.

When using join in a delete statement you have to specify from which tables you want to delete
DELETE c
FROM wp_comments c
JOIN wp_commentmeta m ON c.comment_id = m.comment_id
AND m.meta_key = 'somekey'
AND c.comment_post_ID = '1'

Related

What is the correct syntax?

I wrote a sql query to update data of 'tbl_products'.
Here is the query
update tbl_products
set product_count = (product_count - tbl_order_details.product_sales_quantity)
from tbl_products
join tbl_order_details on tbl_order_details.product_id = tbl_products.product_id
join tbl_order on tbl_order.order_id = tbl_order_details.order_id
where tbl_order.order_id = 54;
But it gives me the following error
"#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 'from tbl_products join tbl_order_details on tbl_order_details.product_id = tbl_p' at line 1"
Whats wrong here?
In MySQL, the correct syntax is:
update tbl_products p join
tbl_order_details od
on od.product_id = p.product_id join
tbl_order o
on o.order_id = od.order_id
set p.product_count = (p.product_count - od.product_sales_quantity)
where o.order_id = 54;

joining table with itself using mysql

When I write this line on mysql on phpmyadmin using mariadb server getting unexpected error near a.cpid = b.cid
select a.cid, a.cname as 'cname', b.cname as 'pname'
from categories as a left OUTER JOIN as b ON a.cpid = b.cid
MySQL said:
#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 'as b ON a.cpid = b.cid LIMIT 0, 25' at line 1
You lack a table name (the second categories in below):
select a.cid, a.cname as 'cname', b.cname as 'pname'
from categories as a
left OUTER JOIN categories as b ON a.cpid = b.cid

Error received on join update, incorrect syntax

I'm receiving an error on the below SQL in MySQL and i'm unsure why. I'm new to MySQL.
UPDATE I
SET I.user_comments = DD.value
FROM
redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
WHERE
user_comments IS NULL
AND allow_create_db = 1
AND user_suspended_time IS NULL
ORDER BY I.user_email
The error is
Error Code: 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 'FROM
redcap_user_information I
JOIN redcap_data D ON D.value = I.use'
Can anyone shed some light?
Not sure about the logic of the join part but the correct syntax for update with join would be as
update redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
set I.user_comments = DD.value
WHERE
user_comments IS NULL --- use the table alias name to access the column
AND allow_create_db = 1 --- use the table alias name to access the column
AND user_suspended_time IS NULL --- use the table alias name to access the column

Delete with join error

After executing this query:
DELETE from swimming_class as tb1 JOIN (SELECT class_id FROM `swimming_class`
left join swimming_school on swimming_school.school_id = swimming_class.school_id
where swimming_school.school_name is NULL) as temp ON temp.class_id = tb1.class_id
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 'as tb1 JOIN (SELECT class_id FROM swimming_class left join swimming_school on' at line 1
I think what you are really after is the following (SQL Fiddle):
DELETE sc
FROM swimming_class sc
INNER JOIN swimming_school ss ON ss.school_id = sc.school_id
WHERE ss.school_name is NULL

Update field inner join

If i run this query i am getting an error:
What do i do wrong?
Query:
UPDATE cscart_profile_fields.value
SET cscart_profile_fields_data.value = '0'
FROM cscart_profile_fields_data
INNER JOIN cscart_user_profiles
ON cscart_profile_fields_data.object_id = cscart_user_profiles.profile_id
WHERE
where cscart_user_profiles.user_id = 5316 and
(
cscart_profile_fields_data.field_id = 65
or
cscart_profile_fields_data.field_id = 66)
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 'FROM cscart_profile_fields_data INNER JOIN cscart_user_profiles ' at line 3
Try this:::
UPDATE cscart_profile_fields_data
INNER JOIN cscart_user_profiles
ON cscart_profile_fields_data.object_id = cscart_user_profiles.profile_id
SET cscart_profile_fields_data.value = '0'
where cscart_user_profiles.user_id = 5316 and
(
cscart_profile_fields_data.field_id = 65
or
cscart_profile_fields_data.field_id = 66)