Working MySQL Query fails in Perl - mysql

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.

Related

MySQL syntax error regarding WHERE NOT EXISTS

I have a Chef recipe for creating Unix user IDs and deploying them across multiple nodes, to guarantee uniqueness and prevent devs from having to track them themselves. They merely name the application and it is granted a unique ID if an ID for that application doesn't already exist. If one does, it is simply returned to the script and user accounts are created on the webservers with the appropriate value.
I have a mysql database with a single table, called application_id_table which has two columns, id and application_name. id is autoincrementing and application name cannot be null (and must be unique).
Removing the Ruby from my script and making a couple of substitutions, my sql looks like this:
INSERT INTO application_id_table(application_name) VALUES('andy_test')
WHERE NOT EXISTS (select 1 from application_id_table WHERE
application_name = 'andy_test');
when run, I receive the syntax parsing 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 'WHERE NOT EXISTS (select 1 from
application_id_table WHERE application_name = 'a'
I recall seeing that the values statement does not allow a where clause but I don't wish to use a select statement to populate the values as I'm populating them from variables supplied from within Ruby/Chef. Anyone have an idea how to accomplish this?
You want to use insert . . . select:
INSERT INTO application_id_table(application_name)
SELECT aname
FROM (SELECT 'andy_test' as aname) t
WHERE NOT EXISTS (select 1 from application_id_table ait WHERE ait.application_name = t.aname);
You should be able to plug your variable directly into the select statement, the same you would would with the VALUES statement.
Try this:
INSERT INTO application_id_table(application_name)
select 'andy_test'
WHERE NOT EXISTS (select 1 from application_id_table WHERE application_name = 'andy_test');

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get 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 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

mysql insert - select syntax error

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.

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..........

Is it possible through SQL injection to launch an UPDATE/DELETE statement from an INSERT/SELECT statement in MySQL?

Let's take the following vulnerable query ($id not being escaped):
SELECT * FROM table WHERE id = $id
Would it be possible in MySQL 5.x to modify some data through an UPDATE statement which would appear inside the hacked SELECT statement?
I thought about something using benchmark() function:
SELECT * FROM table WHERE id = id OR benchmark(1, (UPDATE ...))
But it doesn't seem to work:
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 'UPDATE ...
Any other possibilities not using stored procedure?
Edit: and nor using multiple queries of course...
Depending on the driver this may pass:
SELECT * FROM table WHERE id = id; UPDATE table ...
Multiple queries.