mysql 5.1 stored procedure limit variable - mysql

I have problem executing this proc in my hosted mysql v-5.1. I cant find the problem or error. please help me out.
DROP PROCEDURE IF EXISTS `fetchTimeLine`;;
CREATE PROCEDURE `fetchTimeLine`(IN `delim` int(10))
BEGIN
PREPARE STMT FROM
" SELECT event_id as event,date,schedule,venue,members,descr,about,
(SELECT src FROM photo WHERE event_id=event LIMIT 0,1) as photo1,
(SELECT src FROM photo WHERE event_id=event LIMIT 1,1) as photo2,
(SELECT src FROM photo WHERE event_id=event LIMIT 2,1) as photo3,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 0,1) as video1,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 1,1) as video2,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 2,1) as video3,
(SELECT src FROM videos WHERE event_id=event LIMIT 0,1) as vsrc1
FROM activities
ORDER BY date desc LIMIT ?,?; ";
SET #START = delim;
SET #LIMIT = 2;
EXECUTE STMT USING #START, #LIMIT;
DEALLOCATE PREPARE STMT;
END;;
error I get:
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 '' at line 4

In MySQL, date is a reserved word, and
you have it in your SELECT clause. Try
`date`
instead.
Apply the same change in your ORDER BY clause.

Related

MySQL LIMT is a subquery?

I want to use a select statement to control the limit of another select query, I cant get it to work, what I have below. The Select in the braces returns an INT.
Im newer to MySQL so im not sure what I should use instead to get this to work.
SELECT *
FROM `tbl_prod`
WHERE prod_id = 32
ORDER BY prod_level ASC , prod_date
LIMIT
(SELECT max_count
FROM Prod_subscription
WHERE prod_id = 32)
You can't write subquery in LIMIT, but you can use dynamic SQL to make your expected result.
SET #num = (
SELECT max_count
FROM Prod_subscription
WHERE prod_id = 32);
PREPARE STMT FROM 'SELECT *
FROM `tbl_prod`
WHERE prod_id = 32
ORDER BY prod_level ASC , prod_date
LIMIT ?';
EXECUTE STMT USING #num;

LIMIT with SKIP Value using SELECT COUNT(*)

So I want to get the median of a numeric column using mySQL.
Here is my code:
SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT SELECT FLOOR(COUNT(*)/2) FROM STATION,1
Where LAT_N is the column name that I want to check and STATION is the table name. I got the following error, But I could not understand which syntax is wrong.
ERROR 1064 (42000) at line 1: 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 '(SELECT FLOOR(COUNT(*)/2) FROM STATION),1' at line 3
Could you help me to find the syntax error? If possible I would like to calculate the median using the same way.
You can use window functions instead:
SELECT LAT_N
FROM (SELECT S.*,
COUNT(*) OVER () AS CNT,
ROW_NUMBER() OVER (ORDER BY LAT_N) AS SEQNUM
FROM STATION S
) S
WHERE SEQNUM = FLOOR(CNT / 2);
you can use prepared statements, as Limit only accepts Numbers
SELECT FLOOR(COUNT(*)/2) INTO #a FROM STATION;
SET #s = 'SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT ?,1';
PREPARE stmt1 FROM #s;
EXECUTE stmt1 USING #a;
DEALLOCATE PREPARE stmt1;

Creating table for union query - syntax error

I need to create a table from the union of two queries.
This query works exactly as I need it to:
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3
Once I add the create statement I begin to get errors
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5)
union
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)
The error is:
#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 '(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER ' at line 1
You can try below query:-
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union all
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)

Mysql Syntax Error when running as prepared statment

I've this stored procedure in which I am using a prepared statment to perform two queries.Here's the procedure.
SET #uid = puserid;
SET #rangee = plimit * 50;
SET #post = 'post';
PREPARE STMT FROM
'
SELECT notifications.postid,victims.victimid,friends.friendsname,notificationrecievers.status
FROM friends,victims,notifications,notificationrecievers
WHERE victims.postid=notifications.postid
AND notificationrecievers.notificationid=notifications.notid
AND notificationrecievers.recieverid=?
AND notifications.type=?
AND friends.friendsid=victims.victimid
AND notificationrecievers.notificationid <=
(
SELECT MAX(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)a
)
AND notificationrecievers.notificationid >=
(
SELECT MIN(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)b
)
ORDER BY notifications.postid DESC;
UPDATE notificationrecievers
SET notificationrecievers.status=1
WHERE notificationrecievers.status=0
AND notificationrecievers.recieverid=?
AND
notificationrecievers.notificationid <=
(
SELECT MAX(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)e
)
AND
notificationrecievers.notificationid >=
(
SELECT min(notid) FROM
(
SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
LIMIT 50 OFFSET ?
)g
);
';
EXECUTE STMT USING #uid,#post,#uid,#rangee,#uid,#rangee,#uid,#uid,#rangee,#uid,#rangee;
When I run this query in the procedure it gives me syntax error.But when I use it directly by giving hard coded values in place of ? than it works fine.Why is this happening? puserid and plimit are the parameters which i pass to this procedure.
This is the 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 ';
UPDATE
notificationrecievers
SET notificationrecievers.st' at line 25

WHILE syntax-error in MySQL

I'm trying to use a while loop in a one-off query on a MySQL (5.1.41-3ubuntu12.10-log) database:
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1) != 0) DO
BEGIN
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1
FROM foo);
UPDATE foo
SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
What this does is while there are multiple records with the same id, update one of them with the next id.
The syntax in the body is correct and the predicate used in the while statement also executes without any trouble on it's own. MySQL returns a syntax error on the beginning of the query:
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 'WHILE ((SELECT count(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM stock_produ' at line 1
I realize this might not be The Right Way of doing things, but this is a very badly (or rather not-at-all) thought out database, so it would be awesome if I could get it to work this way.
Thanks,
Robin
It looks as though you are trying to run this procedural code as an anonymous block. While this works in some databases (like Oracle) it can't be done in MySQL.
If you want to run this then put it in a stored procedure and then call the procedure. Hence:
Create procedure
DELIMITER $$
CREATE PROCEDURE `foo_update_routine`()
BEGIN
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1) != 0)
DO
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1 FROM foo);
UPDATE foo SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
END $$
Call Procedure
CALL `foo_update_routine`;
PS You might want to investigate the HAVING clause for your select statements...