0

I have a database table like this :

ID NAME    MODULE       SITUATION
1  morad   M1           YES
1  morad   M2           NO
4  anas    M1           YES
4  anas    M2           YES
2  sofia   M1           NO
2  sofia   M2           NO

I want to transpose data like this using PHP/MySQL.

ID NAME   M1  M2
1  morad  YES NO
4  anas   YES YES
2  sofia  NO  NO
Strawberry
  • 33,750
  • 13
  • 40
  • 57
learner123
  • 275
  • 5
  • 12
  • `SELECT ID, NAME, MAX(CASE WHEN MODULE='M1' THEN SITUATION END) AS M1, MAX(CASE WHEN MODULE='M2' THEN SITUATION END) AS M2 FROM yourtable GROUP BY ID, NAME` https://www.db-fiddle.com/f/2ug2e3zAUSSw3uYZktnGNd/0 – Nick May 13 '19 at 11:05
  • https://stackoverflow.com/questions/52425297/pivot-mysql-table-returns/52425955#52425955 – Strawberry May 13 '19 at 11:36
  • @Nick Thank you, can I use another function, the max function give me NULL in M1 M2 colums. In my real table I have 3 case of SITUATION column V,NI,1. – learner123 May 13 '19 at 11:41
  • @learner123 I'm not sure I completely understand what you mean. Can you edit the dbfiddle to show the problem and post a new one in the comments here? – Nick May 13 '19 at 11:46
  • @Nick can you see this screenshot please https://pasteboard.co/Ievip8E.png – learner123 May 13 '19 at 12:07
  • @learner123 without seeing the raw table data as well the output doesn't mean anything. It's obviously a big table, but perhaps if you put a limit on it you can still demonstrate the problem. – Nick May 13 '19 at 12:10

0 Answers0