mtvaran Posted November 13, 2010 Share Posted November 13, 2010 hi guys, i am struggling with cross tab query in mysql. if you have any idea could you please help me? basically i have data into the table like... cid | Q# | marks c1 | 1| 50 c1 | 2 | 50 c1 | 3 | 50 but i need to be the table like... cid | Q1 | Q2 | Q3 c1 | 50 | 50 | 50 Quote Link to comment https://forums.phpfreaks.com/topic/218538-cross-tab-query-in-mysql/ Share on other sites More sharing options...
fenway Posted November 13, 2010 Share Posted November 13, 2010 That requires joining the table to itself as many times as you has Qs. Quote Link to comment https://forums.phpfreaks.com/topic/218538-cross-tab-query-in-mysql/#findComment-1133809 Share on other sites More sharing options...
DavidAM Posted November 13, 2010 Share Posted November 13, 2010 You can accomplish it using GROUP BY but you still need to know how many Q's you have: SELECT cid, SUM(IF(`Q#`=1, marks, 0)) AS Q1, SUM(IF(`Q#`=2, marks, 0)) AS Q2, SUM(IF(`Q#`=3, marks, 0)) AS Q3 FROM tableName GROUP BY cid You could use PHP and another query to determine the number of Q's and dynamically build the SELECT statement. Quote Link to comment https://forums.phpfreaks.com/topic/218538-cross-tab-query-in-mysql/#findComment-1133920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.