ER_PARSE_ERROR don't know how to fix - mysql

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'

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;

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.

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

MySQL create stored procedure fails but all internal queries succeed alone?

I just created a simple database in MySQL, and I am learning how to write stored proc's. I'm familiar with M$SQL and as far as I can see the following should work:
use mydb;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER //
CREATE PROCEDURE mydb.doStats ()
BEGIN
CREATE TABLE IF NOT EXISTS resultprobability (
ballNumber INT NOT NULL ,
probability FLOAT NULL,
PRIMARY KEY (ballNumber) );
CREATE TABLE IF NOT EXISTS drawProbability (
drawDate DATE NOT NULL ,
ball1 INT NULL ,
ball2 INT NULL ,
ball3 INT NULL ,
ball4 INT NULL ,
ball5 INT NULL ,
ball6 INT NULL ,
ball7 INT NULL ,
score FLOAT NULL ,
PRIMARY KEY (drawDate) );
TRUNCATE TABLE resultprobability;
TRUNCATE TABLE drawprobability;
INSERT INTO resultprobability (ballNumber, probability)
(select resultset.ballNumber ballNumber,(count(0)/(select count(0) from resultset)) probability
from resultset
group by resultset.ballNumber);
INSERT INTO drawProbability (drawDate, ball1, ball2, ball3, ball4, ball5, ball6, ball7, score)
(select distinct r.drawDate, a.ballnumber ball1, b.ballnumber ball2,
c.ballnumber ball3, d.ballnumber ball4, e.ballnumber ball5,
f.ballnumber ball6,g.ballnumber ball7,
((a.probability + b.probability + c.probability + d.probability + e.probability + f.probability + g.probability)/7) score
from resultset r
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 1) a on a.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 2) b on b.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 3) c on c.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 4) d on d.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 5) e on e.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 6) f on f.drawdate = r.drawDate
inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 7) g on g.drawdate = r.drawDate
order by score desc);
END
//
DELIMITER ;
instead i get the following
Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1
Error code 1064, SQL state 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 '' at line 26
Line 6, column 1
Error code 1064, SQL state 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 ')) probability
from resultset
group by resultset.ballNumber);
INSERT INTO d' at line 1
Line 31, column 51
Error code 1064, SQL state 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 ') score
from resultset r
inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114
Execution finished after 0.002 s, 3 error(s) occurred.
What am I doing wrong? I seem to have exhausted my limited mental abilities!
After following suggestions about removing the insert ... select mapping and re-run, I get the following error
Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1
Error code 1064, SQL state 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 '' at line 26
Line 6, column 1
Error code 1064, SQL state 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 ')) probability
from resultset
group by resultset.ballNumber);
INSERT INTO d' at line 1
Line 31, column 51
Error code 1064, SQL state 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 ') score
from resultset r
inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114
Execution finished after 0.002 s, 3 error(s) occurred.
"all internal queries succeed alone"
No, they don't, the first usual supect here failed, the:
INSERT INTO drawProbability (...) (select...
Lose the '(' before select, rerun, retest, solve next syntax problem. Being eager for a solution is no reason to skip basic error checks and especially do not claim to have run them if you didn't.