mysql insert - select syntax error - mysql

This is my query -
$q = "CREATE TEMPORARY TABLE tmp SELECT * from category c WHERE category_id IN (SELECT category_id FROM category_to_store WHERE store_id = '".(int)$from_store_id."');
ALTER TABLE tmp drop category_id;
INSERT INTO category SELECT 0,tmp.* FROM tmp;
SET #last_id_in_category := LAST_INSERT_ID();
select #last_id_in_category;
DROP TABLE tmp;"
mysql_query($q);
I am getting this error on execution
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 'ALTER TABLE tmp drop category_id; INSERT INTO category SELECT 0'
at line 2 Error No: 1064
But when I run the query in database directly then I am not getting any error.
Please help me !

From the php docu on `mysql_query'
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database ...
So basically split your queries to multiple calls of `mysql_query' each with a single query and you should be fine.

Related

How to upsert the result of a stored-procedure into a table by using a stored-procedure in mySql

When I run a stored-procedure ranking_long() by mentioning call ranking_long() in a stored-procedure, the result is as follows.
product_id plg_count rank
11 6962271 1
10 2705517 2
379 1955067 3
378 196865 4
...........
Now I need to upsert(update when there is the same product_id or insert when there is not the same product_id) the result above into the table called dtb_ranking that has the same structure with the above result (product_id, plg_count, rank) by using a stored-procedure. So I tried the stored-procedure below,
INSERT INTO dtb_ranking (`product_id`,`plg_count`,`rank`) VALUES (CALL `ranking_long`()) ON DUPLICATE KEY UPDATE plg_count = VALUES(plg_count), rank = VALUES(rank);
And I had an error message like below.
One or more errors have occurred while processing your request:
Failed to execute a query.:
CREATE DEFINER=xxxxxxx#% PROCEDURE call_ranking_long()
NOT DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
INSERT INTO dtb_ranking
(product_id,plg_count,rank)
VALUES (CALL ranking_long())
ON DUPLICATE KEY UPDATE plg_count = VALUES(plg_count), rank = VALUES(rank);
MySQL's message: #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 'CALL ranking_long()) ON DUPLICATE KEY UPDATE
plg_count = VALUES(plg_count), ra' at line 1
I would appreciate if anyone could tell me what I should do with the script above(INSERT INTO dtb_ranking...).

Unable to delete rows based on key in another table

This is working:
select * FROM Work_Request A where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
As per Delete all rows in a table based on another table this delete should also work but instead giving error:
delete FROM Work_Request A where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
Error:
ERROR 1064 (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 'A where EXISTS ..
...
your delete statement should be like this
delete A.* FROM Work_Request A
where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
you are missing name of table thats gonna delete ...have fun :-)

Mysql statement syntax error with truncate and alter

This is probably very simple but why cant i run this script? Gat a syntax error appear. If i run these separate it works, why?
TRUNCATE TABLE table1;
ALTER TABLE table1 AUTO_INCREMENT = 25;
error
Failed to execute SQL : SQL TRUNCATE TABLE table1; ALTER TABLE table1 AUTO_INCREMENT = 25; failed : 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 'ALTER TABLE table1 AUTO_INCREMENT = 25' at line 1
it will work only if you execute it one after another
i.e.
TRUNCATE TABLE table1;
and then
ALTER TABLE table1 AUTO_INCREMENT = 25;
don't execute together.
if you execute it together first query executed, but second will raise an error.

Working MySQL Query fails in Perl

I have a query, which when run from within phpMyAdmin, it works, however when integrated within a website written in Perl, it fails and throws errors.
Both are run with a connection to the same database, and from coding/formatting side when integrated into the Website, all is correct and the same as other queries.
Any help with this would be much appreciated - Thanks!
MySQL Query:
CREATE TEMPORARY TABLE tmp_lecture_days (
timeslot_id int(50)
);
INSERT INTO tmp_lecture_days (timeslot_id)
SELECT DISTINCT tab_appointment.timeslot_id
FROM tab_appointment WHERE lecture_id = '1115';
SELECT COUNT(timeslot_id)
FROM tmp_lecture_days;
MySQL Query in Perl:
$query
= &statement_database(
"CREATE TEMPORARY TABLE tmp_lecture_days (
timeslot_id int(50)
);
INSERT INTO tmp_lecture_days (timeslot_id)
SELECT DISTINCT tab_appointment.timeslot_id
FROM tab_appointment WHERE lecture_id = '1115';
SELECT COUNT(timeslot_id)
FROM tmp_lecture_days;");
my ($days) = $query->fetchrow_array;
Error Log:
7.3.2011 10:14:12 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 ';
7.3.2011 10:14:12 error INSERT INTO tmp_lecture_days (timeslot_id)
7.3.2011 10:14:12 error SELECT DISTINCT tab_appoi' at line 3
If you are using DBI for running the query, you are not allowed to put more than one statement into it. Try putting CREATE TABLE ... into one query and INSERT ... into another.
I think the following query is equivalent to the 3 queries:
SELECT COUNT(DISTINCT timeslot_id) FROM tab_appointment
WHERE lecture_id = '1115'
For more details, check the MySQL reference manual for COUNT() here.

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
ERROR 1064 (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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........