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开发者_JAVA百科 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.
精彩评论