Is there a way to increment this counter but not display it in the results? I need to add more text before I can submit this question.
SET @counter = 0;
SELECT
@counter:=@counter + 1 AS newindex, -- increment the counter that is in the header
survey_report.id,
survey_report.survey_row_id as respondent_id, -- the id that copied from the survey table
survey_report.qid,
question,
IF(type IN ('S' , 'K'),
(SELECT answer
FROM survey_report
WHERE qid NOT IN (SELECT qid FROM survey_answers)
AND survey_questions.language = lang
AND survey_report.id = @counter),
(SELECT answer
FROM survey_answers
WHERE survey_questions.qid = survey_answers.qid
AND survey_report.qid = survey_questions.qid
AND survey_report.answer = survey_answers.code
AND survey_answers.language = lang
)
) AS answer
FROM survey_questions
JOIN survey_report ON survey_report.qid = survey_questions.qid
WHERE survey_questions.sid = survey_id
ORDER BY survey_report.survey_row_id, survey_report.id;
You could always wrap your current query as a subquery and just select the columns you want:
SET @counter = 0;
SELECT
t.id,
t.respondent_id,
t.qid,
t.question,
t.answer
FROM
(
SELECT
@counter:=@counter + 1,
survey_report.id,
survey_report.survey_row_id as respondent_id,
survey_report.qid,
question,
...
FROM survey_questions
INNER JOIN survey_report
ON survey_report.qid = survey_questions.qid
WHERE survey_questions.sid = survey_id
) t
ORDER BY t.respondent_id,
t.id;
First you can put the init in the same query. to hide the counter you can use this trick.
SELECT
IF(survey_report.id = (@counter := (@counter:=@counter+1)),survey_report.id,survey_report.id) as id
survey_report.survey_row_id as respondent_id, -- the id that copied from the survey table
survey_report.qid,
question,
IF(type IN ('S' , 'K'),
(SELECT answer
FROM survey_report
WHERE qid NOT IN (SELECT qid FROM survey_answers)
AND survey_questions.language = lang
AND survey_report.id = @counter),
(SELECT answer
FROM survey_answers
WHERE survey_questions.qid = survey_answers.qid
AND survey_report.qid = survey_questions.qid
AND survey_report.answer = survey_answers.code
AND survey_answers.language = lang
)
) AS answer
FROM survey_questions
JOIN survey_report ON survey_report.qid = survey_questions.qid
CROSS JOIN ( SELECT @counter := 0 ) AS init
WHERE survey_questions.sid = survey_id
ORDER BY survey_report.survey_row_id, survey_report.id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.