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

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.

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'

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`

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)