Reputation: 2775
Is there a solution for using variable filter in the select statement for a column name without concatenating it with the query ?.
Not working
BEGIN
Declare StartRow int;
set StartRow = ((PageNumber - 1) * PageSize);
SELECT
t.qid,
t.title,
t.vote,
t.answer_count,
t.creation_date,
t.uid,
t.username,
MAX(CASE WHEN t.constantTagNumber = 1 THEN t.tag END) AS tag1,
MAX(CASE WHEN t.constantTagNumber = 1 THEN t.tid END) AS tid1,
MAX(CASE WHEN t.constantTagNumber = 2 THEN t.tag END) AS tag2,
MAX(CASE WHEN t.constantTagNumber = 2 THEN t.tid END) AS tid2,
MAX(CASE WHEN t.constantTagNumber = 3 THEN t.tag END) AS tag3,
MAX(CASE WHEN t.constantTagNumber = 3 THEN t.tid END) AS tid3,
MAX(CASE WHEN t.constantTagNumber = 4 THEN t.tag END) AS tag4,
MAX(CASE WHEN t.constantTagNumber = 4 THEN t.tid END) AS tid4,
MAX(CASE WHEN t.constantTagNumber = 5 THEN t.tag END) AS tag5,
MAX(CASE WHEN t.constantTagNumber = 5 THEN t.tid END) AS tid5
FROM
(
SELECT
questions.id as qid,
title,
creation_date,
vote,
answer_count,
tags.id as tid,
tag,
username,
users.id as uid,
IF (@prev = qid ,@c := @c + 1,@c := 1) constantTagNumber,
@prev := qid
FROM ( SELECT @prev := 0 ,@c := 1) var,question_tags
INNER JOIN tags ON question_tags.tid = tags.id
RIGHT JOIN questions ON question_tags.qid = questions.id
INNER JOIN users ON users.id=owner_id
ORDER BY qid,tid
) t
GROUP BY t.qid ORDER BY filter desc LIMIT StartRow,PageSize;
END
The problem is only with the last
order by filter
herefilter
is avarchar
variable that I am using as a parameter.
Since filter is a variable ,the above code is not working.
Can anyone suggest an simple solution to make it work.
Make It work means ,I need to use the column name to use as order by from parameter.
BEGIN
Declare StartRow int;
set StartRow = ((PageNumber - 1) * PageSize);
set @sql1=(
SELECT
t.qid,
t.title,
t.vote,
t.answer_count,
t.creation_date,
t.uid,
t.username,
MAX(CASE WHEN t.constantTagNumber = 1 THEN t.tag END) AS tag1,
MAX(CASE WHEN t.constantTagNumber = 1 THEN t.tid END) AS tid1,
MAX(CASE WHEN t.constantTagNumber = 2 THEN t.tag END) AS tag2,
MAX(CASE WHEN t.constantTagNumber = 2 THEN t.tid END) AS tid2,
MAX(CASE WHEN t.constantTagNumber = 3 THEN t.tag END) AS tag3,
MAX(CASE WHEN t.constantTagNumber = 3 THEN t.tid END) AS tid3,
MAX(CASE WHEN t.constantTagNumber = 4 THEN t.tag END) AS tag4,
MAX(CASE WHEN t.constantTagNumber = 4 THEN t.tid END) AS tid4,
MAX(CASE WHEN t.constantTagNumber = 5 THEN t.tag END) AS tag5,
MAX(CASE WHEN t.constantTagNumber = 5 THEN t.tid END) AS tid5
FROM
(
SELECT
questions.id as qid,
title,
creation_date,
vote,
answer_count,
tags.id as tid,
tag,
username,
users.id as uid,
IF (@prev = qid ,@c := @c + 1,@c := 1) constantTagNumber,
@prev := qid
FROM ( SELECT @prev := 0 ,@c := 1) var,question_tags
INNER JOIN tags ON question_tags.tid = tags.id
RIGHT JOIN questions ON question_tags.qid = questions.id
INNER JOIN users ON users.id=owner_id
ORDER BY qid,tid
) t
GROUP BY t.qid);
set @sql=CONCAT(@sql1," ORDER BY ",filter," desc LIMIT", StartRow,",",PageSize);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I Updated the above code ,what am I missing here .
Upvotes: 0
Views: 61
Reputation: 6084
You are getting close: Change your code to create a VARCHAR with your SQL in it, use IF/ELSE logic to concatenate your order by in there. Then pass this SQL to the prepared statement:
SET @sql = "Your SQL";
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Upvotes: 1