Home > Database > Mysql Tutorial > mysql data conversion

mysql data conversion

王林
Release: 2023-05-20 10:20:37
Original
1516 people have browsed it

As data analysis becomes more and more important, data conversion and cleaning become more and more necessary. In the process of data analysis, we usually need to convert data from different formats and sources into the format we need. Among them, MySQL is a popular relational database management system today. This article will introduce how to use MySQL for data transformation.

1. MySQL data types

Before performing data conversion, we need to understand the MySQL data types in order to convert the data types correctly. The following are the main data types supported by MySQL:

  1. Integer types: tinyint, smallint, mediumint, int, bigint
  2. Floating point types: float, double, decimal
  3. Character type: char, varchar, text, longtext
  4. Date and time type: date, time, datetime, timestamp

2. Data import and export

MySQL can import and export a variety of data formats, such as csv, json, xml, etc. In the process of data analysis, csv is a very commonly used format. Here's how to import and export csv files to MySQL.

  1. Import data

To import csv files in MySQL, you can use the LOAD DATA statement. Suppose we have a csv file named "data.csv" with the following content:

name,age,gender
Alice,23,Female
Bob,25,Male
Charlie,28,Male
Copy after login

Then you can use the following SQL statement to import the data into the "users" table in MySQL:

LOAD DATA INFILE '/path/to/data.csv'
INTO TABLE users
FIELDS TERMINATED BY ','
LINES TERMINATED BY '
'
IGNORE 1 ROWS;
Copy after login

The above SQL statement will insert the data in the csv file into the table named "users", and the "name", "age" and "gender" columns will correspond to each row of data in the "data.csv" file. . IGNORE 1 ROWS means to ignore the first row of content in the csv file.

  1. Export data

Export the data in MySQL to a csv file, also using the SELECT ... INTO OUTFILE ... statement. The following SQL statement exports the data in the "users" table in MySQL to the "data.csv" file:

SELECT name, age, gender
INTO OUTFILE '/path/to/data.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '
'
FROM users;
Copy after login

In the above SQL statement, we selected "name", "age" and "gender" " column, and use FIELDS TERMINATED BY ',' and LINES TERMINATED BY '
' to specify the field and row delimiters respectively.

3. Data type conversion

In MySQL, you can use the CAST and CONVERT functions to convert data from one type to another. Here are some common data type conversion examples.

  1. Convert a string to a number

Use the CAST function to convert a string to a number type. For example, the following SQL statement converts the string "123" to an integer:

SELECT CAST('123' AS SIGNED);
Copy after login

The above SQL statement will output the number 123. Similarly, the following SQL statement converts the string "3.14" to a floating point number:

SELECT CAST('3.14' AS DECIMAL(10,2));
Copy after login

The above SQL statement will output the number 3.14.

  1. Convert a number to a string

Using the CAST function, you can also convert a number type to a string type. For example, the following SQL statement converts the number 123 to a string:

SELECT CAST(123 AS CHAR);
Copy after login

The above SQL statement will output the string "123".

  1. Date and time type conversion

The date and time types in MySQL include date, time, datetime and timestamp. You can use the CAST and CONVERT functions to convert datetime types to string types, and string types to datetime types. For example, the following SQL statement converts the datetime type to a string type:

SELECT CAST(NOW() AS CHAR);
Copy after login

The above SQL statement will output a string representation of the current datetime. In addition, the following SQL statement converts the string type to a date time type:

SELECT CAST('2022-01-01 00:00:00' AS DATETIME);
Copy after login

The above SQL statement will output the date time type of "2022-01-01 00:00:00".

4. Data Cleaning

In actual data analysis, data cleaning is often required to ensure the accuracy and standardization of the data. Here are some common data cleaning examples.

  1. Deduplication

Deduplication is a common data cleaning method. In MySQL, you can use the DISTINCT keyword to deduplicate data. The following SQL statement will select a unique gender from the "users" table:

SELECT DISTINCT gender FROM users;
Copy after login
  1. Missing value handling

Handling missing values ​​is one of the important steps in data cleaning. In MySQL, you can use the IFNULL function to replace missing values ​​with default values. For example, the following SQL statement replaces missing ages in the "users" table with -1:

SELECT name, IFNULL(age, -1) AS age, gender FROM users;
Copy after login

The above SQL statement will output a list of names, ages, and genders, or if the "age" column is empty, it will Replace with -1.

  1. Data Grouping

Data grouping is a common data cleaning method that can divide data into multiple groups based on certain characteristics. In MySQL, you can use the GROUP BY keyword to group data. The following SQL statement groups the "users" table by gender and calculates the number of people in each group:

SELECT gender, COUNT(*) AS count FROM users GROUP BY gender;
Copy after login

will output statistical data for each gender, for example, "Female" has 1 person, "Male" There were 2 people waiting.

Summary

This article introduces how to use MySQL for data transformation, including data import and export, data types and data cleaning. Mastering these skills can help improve the efficiency and accuracy of data analysis. In practical applications, especially when processing large-scale data, we need to carefully select appropriate data types, transformation methods, and cleaning strategies to ensure that the results of data analysis are accurate and reliable.

The above is the detailed content of mysql data conversion. For more information, please follow other related articles on the PHP Chinese website!

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