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
Related
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;
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)
Trying my first sub query and cant seem to get the right syntax, tried many variations, not sure if i should use temp table or not. Im using this as a guide:
Source
#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 'WHERE log.user_id = 16)AS tempTable' at line 11
SQL
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log_id FROM
(
SELECT log.log_id
FROM log
ORDER BY log.log_id DESC
LIMIT 1
WHERE log.user_id = 16 //<- line 11
)
AS tempTable
)
Your statements are out of order. Also, you don't need IN, you can just use =.
More importantly, you don't need a subquery at all, because MySQL supports ORDER BY and LIMIT in UPDATE queries:
UPDATE log
SET log.out_datetime = NOW()
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1;
It should be change as below.
Correct SQL syntax:
SELECT field-names FROM table-name WHERE field-name=value ORDER BY field-name DESC LIMIT number-of-result
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log_id FROM
(
SELECT log.log_id
FROM log
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1
)
AS tempTable
)
Try the code below. WHERE should come before the LIMIT or ORDER BY clause --
UPDATE log
SET log.out_datetime = NOW()
WHERE log.log_id IN
(
SELECT log.log_id
FROM log
WHERE log.user_id = 16
ORDER BY log.log_id DESC
LIMIT 1
)
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.
I have a simple MySQL query like this:
SELECT * ,
( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) )
+ ( table.get LIKE '%playstation%') AS _score
FROM table
JOIN users on table.id_user = users.id
WHERE table.expire_datetime > 1375997618
HAVING _score > 0
ORDER BY RAND(table.id) ,_score DESC ;
If I run this query in MySQL, it returns usually more then 1 record, now I would like to LIMIT 1 and get one of them randomly, not always the same record.
Is it possible?
select * from <my_table>
ORDER BY RAND()
LIMIT 4
You would quit seeding the random number generator. My guess is that it is returning the first table id encountered, so the numbers are generated in the same sequence:
SELECT * ,
( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) )
+ ( table.get LIKE '%playstation%') AS _score
FROM table
JOIN users on table.id_user = users.id
WHERE table.expire_datetime > 1375997618
HAVING _score > 0
ORDER BY RAND()
LIMIT 1;
As I understand problem in ,_score ?
Try this:
Select * FROM (
***your sql query***
) as t
ORDER BY RAND()
LIMIT 1