First, let me give you an example (if you don’t want to read it, just read the summary below): order_type: order type open_id: user unique identifier SELECT date(create_time) AS 'today's date', sum(real_price) AS 'Total revenue for the day', The if judgment condition is used in the sum function: sum( IF (order_type = 0, real_price, 0) ) AS 'Pay income on the same day', sum( IF (order_type = 1, real_price, 0) ) AS 'Reward income for the day', } count(DISTINCT open_id) AS 'Total number of paying customers', The count function uses the if condition: count( DISTINCT open_id, IF (order_type = 0, TRUE, NULL) ) AS 'Number of payees', count( DISTINCT open_id, IF (order_type = 1, TRUE, NULL) ) AS 'Number of Rewards', } count(id) AS 'Total number of paid orders', The count function uses the if condition: count( DISTINCT id, IF (order_type = 0, TRUE, NULL) ) AS 'Number of payment orders', count( DISTINCT id, IF (order_type = 1, TRUE, NULL) ) AS 'Number of reward orders' } FROM orders WHERE 'real_price' != 1 AND 'status' != 0 GROUP BY DATE(create_time) Query results: In order to distinguish the data statistics of reward orders and payment orders, make the data clearer. summary: The format of if judgment condition in sum function is: sum(if(condition, column value, 0))
1. Single conditional judgment format, sum(if(conditional field name = value, field name to be summed, 0)) 2. Multiple condition judgment format, sum(if(condition field name>value AND condition field name>value AND condition field name=value,1,0))
3. Common case when format, sum(case when condition field name in (smaller range value, larger range value) then [field name to calculate sum] else 0 end) The if conditional format used in the count function is: 1. Count the total number, count(if(condition field name = value, true, null)) 2. Count the total number of duplicate values, count(DISTINCT field name to be counted, if(condition field name = value, true, null)) Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: How to check PCIe version and speed in Linux
>>: Detailed explanation of NodeJS modularity
This article shares the specific code for the WeC...
Preface Fix the footer area at the bottom. No mat...
The json data must be returned in html format That...
This article describes MySQL 8.0 user and role ma...
Part 1 Overview of SSH Port Forwarding When you a...
The img tag in XHTML should be written like this:...
Preface During my internship at the company, I us...
I am very happy to attend this episode of potato ...
Table of contents 1. Problem Description 2. Probl...
This article shares with you how to use Vue to im...
This article example shares the specific code of ...
I have encountered many problems in learning Dock...
Screen Introduction Screen is a free software dev...
This article example shares the specific code of ...
1. Introduction to mysqldump mysqldump is a logic...