Example Table -
nid | sid | cid | data |
938 | 6 | 1 | Sumit |
938 | 6 | 2 | sau@gmail.com |
938 | 6 | 3 | 9878990677 |
938 | 7 | 1 | Priya |
938 | 7 | 2 | priya@gmail .com |
938 | 7 | 3 | 9878990677 |
938 | 8 | 1 | kishan |
938 | 8 | 2 | kishan@gmail.com |
938 | 8 | 3 | 9878990677 |
938 | 9 | 1 | Sunil |
938 | 9 | 2 | sunil@gmail.com |
938 | 9 | 3 | 9878990677 |
938 | 10 | 1 | Vika |
938 | 10 | 2 | vik@gmail.com |
938 | 10 | 3 | 9878990677 |
938 | 11 | 1 | Jain |
938 | 11 | 2 | sab@gmail.com |
938 | 11 | 3 | 9878990677 |
938 | 12 | 1 | Johari |
938 | 12 | 2 | Johari@gmail.com |
938 | 12 | 3 | 9878990677 |
Now we want to
sid | Name | Mobile | |
6 | Sumit | sau@gmail.com | 9878990677 |
7 | Priya | priya@gmail .com | 9878990677 |
8 | kishan | kishan@gmail.com | 9878990677 |
9 | Sunil | sunil@gmail.com | 9878990677 |
10 | Vika | vik@gmail.com | 9878990677 |
11 | Jain | sab@gmail.com | 9878990677 |
12 | Johari | Johari@gmail.com | 9878990677 |
SELECT wsd.sid,
MAX(
IF( wsd.cid =1, wsd.data, NULL )
) AS `Name` ,
MAX(
IF( wsd.cid =2, wsd.data, NULL )
) AS `Email` ,
MAX(
IF( wsd.cid =3, wsd.data, NULL )
) AS `Mobile`
FROM webform_submitted_data wsd
WHERE nid =938 Group By sid
Explanation-
IF(condition, value1, value2)
if condition = value met than its print single value in row other two value will be null
Null value is zero
So we use max for highest value its print all value in single row
IF(condition, value1, value2)
if condition = value met than its print single value in row other two value will be null
Null value is zero
So we use max for highest value its print all value in single row