The difference between count(1) and count(*), ROUND function

1. The difference between count(1) and count(*)

What is the difference between COUNT(*) and COUNT(1)?

count(*) includes all columns, which is equivalent to the number of rows. When calculating the results, NULL values ​​will not be ignored.

count(1) includes ignoring all columns, using 1 to represent code lines, and NULL values ​​will not be ignored when counting results.

count (column name) only includes the column name. When counting results, the count of empty column values ​​(the empty here does not refer to empty strings or 0, but to null) will be ignored, that is, a certain field value is When NULL, no statistics will be collected.

create table counttest(name char(1),age char(2)); 
insert into counttest values('a', '14'),('a', '15'),('a', '15'),
('b', NULL),('b', '16'),('c', '17'),('d', null),('e',''); 

SELECT 
	name,
	count( name ),
	count( 1 ),
	count(*),
	count( age ),
	count(DISTINCT ( age )) 
FROM
	counttest group BY name;

Insert image description here

Insert image description here

2. ROUND function

The ROUND function in MySQL is a function used to round a value to a specified number of decimal places. Its syntax is as follows:

ROUND(x, d)

where x is the value to be rounded and d is the number of decimal places to be retained. If d is a positive number, then x will be rounded to d decimal places; if d is a negative number, x will be rounded to the dth decimal place.

Here are some examples:

  1. ROUND(3.14159, 2) returns 3.14. Here 3.14159 is rounded to 2 decimal places.
  2. ROUND(12.345, 0) returns 12. Here 12.345 is rounded to the integer part.
  3. ROUND(123.456, -1) returns 120. Here 123.456 is rounded to the first digit of the integer part.

It should be noted that the rounding rule of the ROUND function is standard rounding. If the next decimal place is 5, it will be rounded up, otherwise it will be rounded down.

In addition, there is an optional parameter mode that can be used to specify the rounding mode. The default is 0, which means rounding. If mode is a positive number, it means rounding up; if it is a negative number, it means rounding down.

All in all, the ROUND function in MySQL provides a convenient way to round numerical values.

SELECT
	gender,
	university,
	COUNT(*) user_num,
	ROUND ( AVG( active_days_within_30 ), 1 ) avg_active_days,
	ROUND ( AVG( question_cnt ), 1 ) avg_quesition_cnt 
FROM
	user_profile 
GROUP BY
	gender,
	university

3. SQL19 group filtering exercises

Group filtering exercises

Insert image description here

SELECT
	university,
	avg( question_cnt ) avg_question_cnt,
	avg( answer_cnt ) avg_answer_cnt 
FROM
	user_profile 
GROUP BY
	university 
HAVING
	avg_question_cnt < 5 
	OR avg_answer_cnt < 20;
DROP TABLE IF EXISTS user_profile;
CREATE TABLE `user_profile` (
	`id` INT NOT NULL,
	`device_id` INT NOT NULL,
	`gender` VARCHAR ( 14 ) NOT NULL,
	`age` INT,
	`university` VARCHAR ( 32 ) NOT NULL,
	`gpa` FLOAT,
	`active_days_within_30` INT,
	`question_cnt` FLOAT,
	`answer_cnt` FLOAT 
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);

复制
输出:
university|avg_question_cnt|avg_answer_cnt
北京大学    |2.500           |21.000
浙江大学    |1.000           |2.000

4. The difference between Mysql bigdecimal and float

In MySQL, BigDecimal and Float are two different data types used to store and process numeric data. They have the following important differences:

  1. Precision: BigDecimal is a high-precision numeric type that can store arbitrary precision values, while Float is a single-precision floating-point type with a fixed digit limit. In general, BigDecimal can store a larger range of values ​​and retain higher precision.

  2. Rounding: Due to the limitations of floating-point numbers in binary representation, Float type values ​​may produce rounding errors when calculated and stored. This means that BigDecimal is generally more reliable than Float when doing precise calculations, because BigDecimal uses a decimal-based representation that allows for precise rounding.

  3. Storage space: Float type data usually takes up less storage space than BigDecimal. For large amounts of numeric data, using the Float type may save storage space.

  4. Computing performance: Since BigDecimal is based on BigInteger, calculations may consume more computing resources than the Float type. The Float type can use hardware floating point operation instructions for fast calculations, while BigDecimal requires more complex algorithm calculations.

To sum up, when precise calculations and high-precision storage are required, especially in the financial field or scenarios that require precise calculations, it is recommended to use BigDecimal. When the accuracy requirements are not high, and you need to save storage space and improve computing performance, you can consider using the Float type.

5. Implicit inner joins and explicit inner joins (INNER can be omitted)

Insert image description here
Insert image description here

-- 隐式内连接
SELECT
	q.device_id,
	q.question_id,
	q.result 
FROM
	question_practice_detail q,
	user_profile u 
WHERE
	q.device_id = u.device_id 
	AND u.university = '浙江大学';
	
-- 显示内连接 (INNER可省略)
SELECT
	q.device_id,
	q.question_id,
	q.result 
FROM
	question_practice_detail q
	INNER JOIN user_profile u
	ON q.device_id = u.device_id 
WHERE
	u.university = '浙江大学';

Guess you like

Origin blog.csdn.net/Blue_Pepsi_Cola/article/details/132644373
Recommended