Home  >  Article  >  Backend Development  >  mysql query statement problem

mysql query statement problem

WBOY
WBOYOriginal
2016-12-01 01:27:321078browse

Picture 1:mysql query statement problem

Picture 2:mysql query statement problem

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

Reply content:

Picture 1:mysql query statement problem

Picture 2:mysql query statement problem

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn