Home > Database > Mysql Tutorial > How to use mysql to complete data generation in excel

How to use mysql to complete data generation in excel

小云云
Release: 2017-12-08 09:44:21
Original
1395 people have browsed it

Excel is the most commonly used tool in data analysis. This article compares the functions of mysql and excel to introduce how to use mysql to complete data generation, data cleaning, preprocessing, and the most common data classification and data filtering in excel. Classification, aggregation, and data pivot operations. In this article, we introduce parts 5, 6, and 7, data extraction, data screening, and data summary and perspective. This article mainly introduces relevant information on how to use mysql to complete data generation in excel. Friends in need can refer to it.

5. Data extraction

The fifth part is data extraction, which is also the most common task in data analysis. The following describes how to use each function.

Extract data by columns

#Extract data by columns SELECT city FROM data1;

Extract data by rows

#按行提取
SELECT * FROM data1 WHERE city='beijing';
Copy after login

Extract data by location

#按位置提取
SELECT * FROM data1 LIMIT 2,5;
Copy after login

Extract data by conditions

#按条件提取并计算
SELECT AVG(price) FROM data1 WHERE city=&#39;beijing&#39; AND age<25;
Copy after login

6, data filtering

The sixth part is data filtering. Use the three conditions of AND, OR, NOT and greater than, less than and equal to filter the data, and perform counting and summing. Similar to the filtering function and countifs and sumifs functions in excel.

Filter by conditions (AND, OR, NOT)

The Excel data directory provides a "Filter" function, which is used to filter data tables according to different conditions. WHERE is used in mysql to complete the filtering operation. With the sum and count functions, it can also realize the functions of sumif and countif functions in excel.

#数据筛选AND
SELECT * FROM data1 WHERE city=&#39;shanghai&#39; AND age>30;
Copy after login

##

#数据筛选IN
SELECT * FROM data1 WHERE city IN (&#39;shanghai&#39;,&#39;beijing&#39;);
Copy after login

#数据筛选OR
SELECT * FROM data1 WHERE city=&#39;shanghai&#39; OR age>30;
Copy after login


##
#数据筛选(不等于)
SELECT * FROM data1 WHERE city !=&#39;beijing&#39;;
Copy after login


##

#数据筛选like(模糊筛选)
SELECT * FROM data1 WHERE city LIKE &#39;bei%&#39;;
Copy after login

##

#筛选后计数 countif
SELECT COUNT(id) AS id_count FROM data1 WHERE city=&#39;shanghai&#39;AND age>30;
Copy after login


##
#筛选后求和 sumtif
SELECT SUM(price) AS price FROM data1 WHERE city=&#39;beijing&#39; AND age<30;
Copy after login


#筛选后求均值 averageif
SELECT AVG(price) AS avg_price FROM data1 WHERE city !=&#39;beijing&#39;;
Copy after login

##7, Data classification, summary and perspective

The seventh part is to classify and summarize the data. In Excel, the data can be summarized according to specific dimensions using classified summarization and pivot data. The main functions used in mysql are GROUP BY and CASE WHEN. The following describes how to use these two functions.

Classification and Summary

Excel's data directory provides the "Classification and Summary" function, which can summarize the data table according to the specified fields and summary methods. The corresponding operations are completed through GROUP BY in mysql, and multi-level classification and aggregation can be supported.

GROUP BY is a function for classification and aggregation. The method of use is very simple. Just specify the column name to be grouped. You can also specify multiple column names at the same time. GROUP BY press Column names are grouped in the order in which they appear. At the same time, it is necessary to formulate a summary method after grouping. The two common ones are counting and summing.

#单列分类汇总
SELECT city,COUNT(id) AS id_count FROM data1 GROUP BY city ORDER BY id_count;
Copy after login

##

#多列分类汇总
SELECT city,colour,ROUND(SUM(price),2) AS id_count FROM data1 GROUP BY city,colour;
Copy after login

PivotTable


The "PivotTable" function is provided under the insert directory in Excel to summarize the data table according to specific dimensions. Pivot table functionality is not directly provided in mysql. But the same effect is achieved through the CASE WHEN function.

数据透视表也是常用的一种数据分类汇总方式,并且功能上比GROUP BY要强大一些。下面的代码中设定city为行字段,colour为列字段,price为值字段,计算price金额。


#查看原始数据表
SELECT * FROM data1;
Copy after login


#使用CASE WHEN进行数据透视
CREATE VIEW data_Items AS (
 SELECT
 data1.city,
 CASE WHEN colour = "A" THEN price END AS A,
 CASE WHEN colour = "B" THEN price END AS B,
 CASE WHEN colour = "C" THEN price END AS C,
 CASE WHEN colour = "F" THEN price END AS F
 FROM data1
);
Copy after login


#查看结果
SELECT * FROM data_Items;
Copy after login


#对字段进行求和汇总
CREATE VIEW data1_Extended_Pivot AS (
 SELECT
 city,
 SUM(A) AS A,
 SUM(B) AS B,
 SUM(C) AS C,
 SUM(F) AS F
 FROM data_Items
 GROUP BY city
);
Copy after login


#查看结果
SELECT * FROM data1_Extended_Pivot;
Copy after login


#对空值进行处理
CREATE VIEW data1_Extended_Pivot_Pretty AS (
 SELECT 
 city, 
 COALESCE(A, 0) AS A, 
 COALESCE(B, 0) AS B, 
 COALESCE(C, 0) AS C,
 COALESCE(F, 0) AS F
 FROM data1_Extended_Pivot
);
Copy after login


#查看数据透视结果
SELECT * FROM data1_Extended_Pivot_Pretty;
Copy after login

大家在开发过程中可能会经常遇到,现在就跟着本文尝试操作一下吧。

相关推荐:

PHP导出EXCEL快速开发指南

Python操作excel文件的案例

php使用原生的方法导出excel实例分享

The above is the detailed content of How to use mysql to complete data generation in excel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template