Home > Article > Backend Development > mysql query statement problem
Picture 1:
Picture 2:
Question: How to query Figure 1 through SQL statements and display the format as shown in Figure 2? (PS: There are many more orderids, not just 6). Is it because the SQL statement cannot be processed and can only be processed through PHP? Please give me some advice, I've been thinking about it for a long time
Picture 1:
Picture 2:
Question: How to query Figure 1 through SQL statements and display the format as shown in Figure 2? (PS: There are many more orderids, not just 6). Is it because the SQL statement cannot be processed and can only be processed through PHP? Please give me some advice, I've been thinking about it for a long time
Try the following sql statement to see if it meets your needs
<code>select orderid,sum(CASE cosetypeid WHEN 4 THEN amount END) AS 'cosetypeid为4时的amount值', sum(CASE cosetypeid WHEN 5 THEN amount END) AS 'cosetypeid为5时的amount值', sum(CASE cosetypeid WHEN 7 THEN amount END) AS 'cosetypeid为7时的amount值' from X GROUP BY orderid </code>
Of course it depends on the back-end code to write. The database only handles the writing and reading of data. The logic code still needs to be done by the backend, that is, PHP. There is no need to force MYSQL to do this work.
You can try it upstairs