Mysql select dynamic row values as column name from a table and value from another table

Riyadh Ahmed

I have arranged a survey where a project has some questions. Users have to collect the given project answer from public. Survey tables like

user tables

user_id     user_name
  1          User 1   
  2          User 2

Project table

project_id  project_name
  1           project 1    
  2           project 2

Question table

ques_id  project_id  ques_name
  1         1         Question 1
  2         1         Question 2 
  3         1         Question 3
  4         1         Question 4 

Answer table

ans_id  public_id  user_id ques_id  ques_ans
  1         1      1         1     Answer 1
  2         1      1         2     Answer 2 
  3         1      1         3     Answer 3
  4         1      1         4     Answer 4 

Now i want to generate a reports where question table values as column name matched by given project_id and question answers as value from answer table matched by ques_id

And, her is my expected output:

User_Name  public_id  Question 1  Question 2  Question 3  ...
User 1        1         Answer 1   Answer 2    Answer 3   ...

Someone suggested to use pivot but i found "MySQL doesn't have native support for pivoting operations" can anyone help me out?

Riyadh Ahmed

Finally the code is working

SET @sql = NULL;
    SELECT
    GROUP_CONCAT(DISTINCT
    CONCAT(
    'MAX(IF(a.ques_id = ',
    ques_id,
    ', a.ques_ans, NULL)) AS `',
    ques_name,'`'
    )
    ) INTO @sql
     FROM survey_answer inner join survey_question on  survey_answer.ques_id=survey_question.id;

set @sql = CONCAT('select u.user_name ,q.category_id,a.p_code, ' ,@sql,'  FROM `survey_answer` as a
LEFT JOIN `users` as u ON a.user_id = u.user_id
LEFT JOIN `survey_question` as q ON a.ques_id= q.id');
 PREPARE stmt FROM @sql;
 EXECUTE stmt;
 DEALLOCATE PREPARE stmt;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to insert row into a table by select column name and value from another table in SQL Server

If name exists in a table then return value from another column or row

Select specific row from mysql table based on a column value

PostgreSQL dynamic query comparing row from table to column name of another table

Query to select value from another table as a column

Pandas: Mapping column name from a particular table to a row in another table

Update column in SQL table with values from another row in same table

Filling a resultant table with corresponding values (row and column) from another table

mysql update column with value from another table

mysql update column value from another table

Get specific value from table with table name, row and column index

MSSQL Select column values from one table to another table

Insert new row into a table with values from another table mysql

mysql insert into select join - copy values from one column to another table, passing through a connecting table

DROP TABLE by CONCAT table name with VALUE from another SELECT [SQLite]

Select a specific row with additional column that its value is from another column in one table

SQL replace all row values in view column by value from column of another table

Update column value of one table based on values from another table

mySQL SELECT values from a TABLE where the TABLE name is based on a field value

SQL insert data dynamic column name from another table

Select records from table where a given column value has no non-null values in another column

Mysql select from table by comparing value in another table

MySQL select from a table and check in another table if the same value exists

Row value from another table

Select a value from a row that is matched to another value in the same table

Select all rows from a table except where row in another table with same id has a particular value in another column

Getting id values from a column and then get value from another table

SQL Server : select all rows where Column does not contain any value from dynamic table of values

mysql insert unique values from one column to a column of another table