Incorrect CASE syntax - mysql

Here’s my query:
SELECT
CASE WHEN COUNT(o) > 0 THEN true ELSE false
FROM orders o
left join shop_order so on o.id=so.order_id
left join order_details od on so.id=od.shop_order_id
left join offers of on od.offer_id=of.id
WHERE of.offer_type_id=1
and of.type=2
and o.customer_id = ?1
and od.varient_id=?2
the error is :
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 orders o left join shop_order so on o.id=so.order_id left join order_det

The case is missing the end keyword. Insert END before FROM:
SELECT
CASE WHEN COUNT(o) > 0 THEN true ELSE false END
FROM ...
MySQL error messages say “near”, but they really mean “right before”.

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;

Calculating margin through mysql query

My mysql query is as below which give error
Below is with if else
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(IF co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
END IF
) as margin FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;
Below on is with case
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
) as margin FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;
I am getting below error for id
Error Code: 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 'co.retake_flag = 1 THEN ((co.product_amt_disc - (p.admin_price+6))/(co.product' at line 7
I am getting error for CASE
Error Code: 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 margin FROM customer_orders co LEFT JOIN m_products p ON p.' at line 11
I want to calculate percentage on behalf of condition retake flag. please help and let me know if anything else required
Thanks
I was missing END in CASE statement
the correct answer is as below
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc) ) END
) as margin
FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;

MYSQL Error Code: 1064 in the view

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 'LEFT OUTER JOIN dw_fact_claim ON dw_fact_claim.patient_id = dw_snf.patient_id
' at line 13
my query is:
CREATE VIEW DW_SNF
AS
SELECT
REL ,
PATIENT_ID ,
MIN( CLM_FROM_DT )AS clm_from_dt ,
MAX( clm_THRU_dt )AS clm_THRU_dT
FROM dw_SNF
WHERE REL <> ''
GROUP BY
REL ,
PATIENT_ID
LEFT OUTER JOIN dw_fact_claim ON dw_fact_claim.patient_id = dw_snf.patient_id
AND dw_fact_claim.CLM_FROM_DT >= dw_SNF.clm_from_dt
AND dw_fact_claim.CLM_THRU_DT <= dw_SNF.clm_thru_dt
AND dw_fact_claim.TYPE_OF_CLAIM NOT LIKE '%snf%'
WHERE dw_SNF.rel <> ''
GROUP BY
rel ON SUMMARY.REL = SNF_BASE.REL
INNER JOIN dw_VIEW_dim_patient_FLAG AS DW_DIM_PATIENT ON dw_dim_patient.patient_id = dw_snf.patient_id
INNER JOIN DW_VIEW_JH_ACG_CONDENSED dw_jh_acg ON dw_jh_acg.patient_id = dw_dim_patient.patient_id
WHERE SNF_EPISODE_ID <> '';
I have used where clause before left outer join,that's the error was and after using where condition at the end of join conditions i have created view successfully.We can not have JOIN in Group by clause.

How to return null as zero in mysql?

i want to return pending as zero when it's null:
SELECT `t1`.*, `t2`.`pending` as IFNULL(ending, `0)`
FROM (`tblproducts` t1)
LEFT JOIN `cache_invoice` t2 ON `t1`.`id` = `t2`.`product_id`
LEFT JOIN `cache_stock` t3 ON `t1`.`id` = `t3`.`product_id`
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 '(ending, 0) FROM (tblproducts t1) LEFT JOIN cache_invoice
t2 ON t1.id ' at line 1
any idea where the problem is?
The issue is here:
`t2`.`pending` as IFNULL(ending, `0)`
You are checking IFNULL for an alias value of the column, while it should be as following:
IFNULL(`t2`.`pending`, 0) as ending
First evaluate if null - then show 0 and create alias if you want using 'AS' keyword. So, your query will be:
SELECT `t1`.*, IFNULL(`t2`.`pending`, 0) as ending
FROM `tblproducts` t1
LEFT JOIN `cache_invoice` t2 ON `t1`.`id` = `t2`.`product_id`
LEFT JOIN `cache_stock` t3 ON `t1`.`id` = `t3`.`product_id`