Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/218538-cross-tab-query-in-mysql/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.