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
<iframe src=”test.jsp” width=”100″ height=”50″...
In some scenarios, we need to modify our varchar ...
Table of contents Preface vue.config.js configura...
mysql-5.7.17-winx64 is the latest version of MySQ...
Today, due to project requirements, js is needed t...
Set Anchor Point <a name="top"><...
Table of contents 1. Cause 2. Equipment Informati...
We are all familiar with the MySQL count() functi...
MySQL 5.7.17 installation and configuration metho...
Table of contents Implementing HTML Add CSS Imple...
If the table is wide, it may overflow. For exampl...
Nowadays, many websites do not allow direct copyin...
This article example shares the specific code of ...
Deploy database based on docker sudo docker pull ...
1. Check Linux disk status df -lh The lsblk comma...