joining table with itself using mysql - 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

Related

ER_PARSE_ERROR don't know how to fix

select Customer.CustNo, Customer.FirstName, Customer.LastName,
`Order`.OrderNo, `Order`.OrderDate, Employee.EmpNo,
Employee.FirstName, Employee,LastName, Product.ProdNo,
Product.ProdName,
CAST(substring(Product.Price,2) AS FLOAT) * ProductInOrder.Qty AS 'OrderCost'
From `Order`
inner join Employee on `Order`.Employee_EmpNo = Employee.EmpNo
inner join Customer on Customer.CustNo = `Order`Customer_CustNo
inner join ProductInOrder on `Order`.OrderNo = ProductInOder.Order_OrderNo
inner join Product on ProductInOrder.Product_ProdNo = Product.ProductNo
Where Employee.EmpNo is not null
and str_to_date(`Order`.OrderDate,'%Y-%m-%d') = '2007-1-23'
Having OrderCost > 150
Order by OrderCost Desc
After I execute it the 'ER_PARSE_ERROR' error appear.
Full error message:
ER_PARSE_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 'FLOAT) * ProductInOrder.Qty AS 'OrderCost' From Order inner join Employee on `' at line 1'

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;

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

Why the SQL shows MySQL Error?

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'

Syntax error Mysql

No Idea why this is not working - could someone please help?
update c set c.images = ""
from j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like "cakes%"
and c.created_by in (62,63,99)
and rc.email = 'email'
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 j17_content c inner join j17_jreviews_content rc on rc.contentid = c.id in' at line 2
UPDATE:
Now trying
UPDATE j17_content c SET c.images=''
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
still getting
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 'inner join j17_jreviews_content rc on rc.contentid = c.id inner join j17_catego' at line 2
UPDATE doesn't take a FROM.
The syntax should be UPDATE j17_content c SET c.images="" INNER JOIN ...
This is essentailly your code.
Try it this way:
update j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
set c.images = '';
This is an UPDATE JOIN
I have a refactored version of the query in mind
update
(
SELECT content_id id
FROM j17_jreviews_content
WHERE email = 'email'
) rc
inner join j17_content c USING (id)
inner join
(
SELECT id catid
FROM j17_categories
WHERE created_by in (62,63,99)
AND path like 'cakes%'
) cat USING (catid)
set c.images = '';
You will need indexes for the subqueries involved
ALTER TABLE j17_categories ADD INDEX created_by_path_id_ndx (created_by,path,id);
ALTER TABLE j17_jreviews_content ADD INDEX email_content_id_ndx (email,content_id);
replace your double quotes(") with single ones(')
Sorry this is not really the way you guys suggested - but I got a list of IDs and updated them using no joins with an in statement of all the IDs