clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1451375 fiddles created (18702 in the last week).

CREATE TABLE TableData_400 (id INT, created_date DATETIME, sample_1 TEXT, sample_2 TEXT, sample_3 TEXT, sample_4 TEXT, sample_7 TEXT, sample_8_1 text, sample_8_2 text, sample_9 smallint , sample_10 smallint ); INSERT INTO TableData_400 VALUES ('1','2019-05-17 06:19:13','aaa', 'bbb','ccc','ddd','eee','fff','ggg',0,0); select * from TableData_400
id created_date sample_1 sample_2 sample_3 sample_4 sample_7 sample_8_1 sample_8_2 sample_9 sample_10
1 2019-05-17 06:19:13 aaa bbb ccc ddd eee fff ggg 0 0
 hidden batch(es)


CREATE TABLE TableData_elements (Dataid INT, sample_id INT, sample_title TEXT ); INSERT INTO TableData_elements VALUES ('400','1','Fname'), ('400','2','Sname'), ('400','3','Lname'), ('400','4','sample'), ('400','7','sample1'), ('400','8','Name'), ('400','9','checkbox'), ('400','10','checkbox'); select * from TableData_elements where Dataid=400
Dataid sample_id sample_title
400 1 Fname
400 2 Sname
400 3 Lname
400 4 sample
400 7 sample1
400 8 Name
400 9 checkbox
400 10 checkbox
 hidden batch(es)


CREATE TABLE TableData_elements_options (Dataid INT, sample_id INT, option_id INT, optionss TEXT ); INSERT INTO TableData_elements_options VALUES ('400',9,1,'First'), ('400',9,2,'second'), ('400',9,3,'Third'), ('400',10,1,'First'), ('400',10,2,'second'), ('400',10,3,'Third'); select * from TableData_elements_options where Dataid=400
Dataid sample_id option_id optionss
400 9 1 First
400 9 2 second
400 9 3 Third
400 10 1 First
400 10 2 second
400 10 3 Third
 hidden batch(es)


SELECT CONCAT( 'SELECT ', GROUP_CONCAT(CONCAT('sample_', sample_id, ' ', sample_title)), ' FROM TableData_400' ) INTO @sql FROM TableData_elements; SELECT @sql; PREPARE myStatement FROM @sql; EXECUTE myStatement ; DROP PREPARE myStatement;
@sql
SELECT sample_1 Fname,sample_2 Sname,sample_3 Lname,sample_4 sample,sample_7 sample1,sample_8 Name,sample_9 checkbox,sample_10 checkbox FROM TableData_400
 hidden batch(es)


SELECT CONCAT( 'SELECT ', GROUP_CONCAT(CONCAT('sample_', te.sample_id, CASE WHEN teo.option_id>0 THEN CONCAT('_',teo.option_id) ELSE '' END, ' ', sample_title)), ' FROM TableData' ) FROM TableData_elements te LEFT JOIN TableData_elements_options teo ON teo.Dataid=te.Dataid AND teo.sample_id=te.sample_id ;
CONCAT( 'SELECT ', GROUP_CONCAT(CONCAT('sample_', te.sample_id, CASE WHEN teo.option_id>0 THEN CONCAT('_',teo.option_id)
SELECT sample_9_1 checkbox,sample_9_2 checkbox,sample_9_3 checkbox,sample_10_1 checkbox,sample_10_2 checkbox,sample_10_3 checkbox,sample_1 Fname,sample_2 Sname,sample_3 Lname,sample_4 sample,sample_7 sample1,sample_8 Name FROM TableData
 hidden batch(es)


expected result will be like this , if (sample title= check box then no need to select value from options table) SELECT sample_9 'checkbox',sample_10 'checkbox',sample_1 'Fname',sample_2 'Sname',sample_3 'Lname',sample_4 'sample',sample_7 'sample1',sample_8_1 'FName',sample_8_2 'Sname' (if sample title=name take fname and seconad name as _1,_2) FROM TableData
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 'expected result will be like this , if (sample title= check box then no need to' at line 1
 hidden batch(es)