天天看點

sql初學者指南_2020年面向初學者的終極SQL指南。 SQL是什麼意思? (What does SQL Mean?) 句法 (Syntax) 選擇 (Select) 選擇不同 (Select Distinct) 哪裡 (Where) AND,OR和NOT運算符 (AND, OR and NOT Operators) 訂購依據 (Order By) 插 (Insert) 更新資料 (Update) 删除 (Delete) Joins (Joins) 組 (Group)

sql初學者指南

SQL is a standard language for storing, manipulating, and retrieving data in databases. In this article, I’ll teach you the very basic fundamentals of the SQL language and hope you will be able to write your own database queries at the end.

SQL是用于在資料庫中存儲,處理和檢索資料的标準語言。 在本文中,我将向您介紹SQL語言的基本知識,并希望您最終能夠編寫自己的資料庫查詢。

SQL是什麼意思? (What does SQL Mean?)

SQL stands for Structured Query Language and lets you access and manipulate databases.

SQL代表結構化查詢語言,可讓您通路和操作資料庫。

句法 (Syntax)

Most of the actions you need to perform on a database are done with SQL statements. The following SQL statement selects all the records in the “Users” table:

您需要對資料庫執行的大多數操作都是使用SQL語句完成的。 以下SQL語句選擇“使用者”表中的所有記錄:

SELECT * FROM Users;
           

選擇 (Select)

The select statement is used to retrieve data from a database. The requested data is returned in a results table.

select語句用于從資料庫檢索資料。 請求的資料将在結果表中傳回。

SELECT column1 FROM table_name;
           

選擇不同 (Select Distinct)

The Select Distinct statement is used to return only distinct (different) values.

Select Distinct語句用于僅傳回不同的(不同的)值。

SELECT DISTINCT * FROM table_name;
           

Count

計數

The following SQL statement lists the number of different customer countries:

以下SQL語句列出了不同客戶國家/地區的數量:

SELECT COUNT(DISTINCT Country) FROM Customers;
           

哪裡 (Where)

The Where clause is used to filter records.

Where子句用于過濾記錄。

SELECT column1
FROM table_name
WHERE condition;
           

For example:

例如:

SELECT * FROM Users
WHERE Country='Netherlands';
           

AND,OR和NOT運算符 (AND, OR and NOT Operators)

The Where clause can be combined with AND, OR, and NOT operators. The AND and OR operators are used to filter records based on more than one condition:

Where子句可以與AND,OR和NOT運算符結合使用。 AND和OR運算符用于根據多個條件過濾記錄:

  • The AND operator displays a record if all the conditions separated by AND are TRUE.

    如果用AND分隔的所有條件均為TRUE,則AND運算符将顯示一條記錄。

  • The OR operator displays a record if any of the conditions separated by OR is TRUE.

    如果由OR分隔的任何條件為TRUE,則OR運算符将顯示一條記錄。

The NOT operator displays a record if the condition(s) is NOT TRUE.

如果條件不正确,則NOT運算符将顯示一條記錄。

AND

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
           

OR

要麼

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
           

NOT

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
           

訂購依據 (Order By)

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

ORDER BY關鍵字用于按升序或降序對結果集進行排序。

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
           

The following SQL statement selects all users from the “Users” table, sorted DESCENDING by the “Country” column:

以下SQL語句從“使用者”表中選擇所有使用者,并按“國家/地區”列按DESCENDING排序:

SELECT * FROM Users
ORDER BY Country DESC;
           

插 (Insert)

The Insert statement is used to insert new records in a table. It is possible to write the Insert statement in two ways.

Insert語句用于在表中插入新記錄。 可以通過兩種方式編寫Insert語句。

First way

第一種方式

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
           

Second way

第二種方式

INSERT INTO table_name
VALUES (value1, value2, value3, ...);
           

更新資料 (Update)

The Update statement is used to modify the existing records in a table.

Update語句用于修改表中的現有記錄。

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
           

删除 (Delete)

The Delete statement is used to delete existing records in a table.

Delete語句用于删除表中的現有記錄。

DELETE FROM table_name WHERE condition;
           

Joins

(

Joins

)

A Join clause is used to combine rows from two or more tables, based on a related column.

Join子句用于根據相關列組合來自兩個或多個表的行。

Different Types of SQL Joins

不同類型SQL連接配接

Here are the different types of the Joins in SQL:

以下是SQL中Join的不同類型:

  • (INNER) JOIN: Returns records that have matching values in both tables

    (INNER)JOIN :傳回兩個表中具有比對值的記錄

  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table

    LEFT(OUTER)JOIN :傳回左側表中的所有記錄,以及右側表中的比對記錄

  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table

    RIGHT(OUTER)JOIN :從右表傳回所有記錄,并從左表傳回比對的記錄

  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

    FULL(OUTER)JOIN :當左表或右表中存在比對項時,傳回所有記錄

Inner Join

内部聯接

The Inner Join keyword selects records that have matching values in both tables.

内部聯接關鍵字選擇在兩個表中具有比對值的記錄。

SELECT column_name(s)
FROM table1
INNER JOIN table2ON table1.column_name = table2.column_name;
           

Left Join

左加入

The Left Join keyword returns all records from the left table (table1), and the matched records from the right table (table2).

Left Join關鍵字傳回左表(table1)中的所有記錄,以及右表(table2)中的比對記錄。

SELECT column_name(s)
FROM table1
LEFT JOIN table2ON table1.column_name = table2.column_name;
           

Right Join

正确加入

The Right Join keyword returns all records from the right table (table2), and the matched records from the left table (table1).

Right Join關鍵字傳回右表(table2)中的所有記錄,并傳回左表(table1)中的比對記錄。

SELECT column_name(s)
FROM table1
RIGHT JOIN table2ON table1.column_name = table2.column_name;
           

Full Join

完全加入

The Full Outer Join keyword returns all records when there are a match in left (table1) or right (table2) table records.

當左(表1)或右(表2)表記錄比對時,“完全外部聯接”關鍵字将傳回所有記錄。

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2ON table1.column_name = table2.column_nameWHERE condition;
           

組 (Group)

The group by statement groups rows with the same values into summary rows, like “find the number of users in each country.”

group by語句将具有相同值的行分組為摘要行,例如“查找每個國家/地區的使用者數”。

SELECT column_name(s)FROM table_nameWHERE conditionGROUP BY column_name(s)ORDER BY column_name(s);

SELECT 列名(S)FROM TABLE_NAME WHERE 條件 GROUP BY 列名(多個)ORDER BY 列名(多個);

結論 (Conclusion)

After this article, I hope you have learned the basic fundamentals of SQL, and you can write your own creative queries to manage your database!

讀完本文後,希望您已經學習了SQL的基本知識,并且可以編寫自己的創意查詢來管理資料庫!

翻譯自: https://levelup.gitconnected.com/the-ultimate-sql-guide-for-beginners-in-2020-4f13e45f371

sql初學者指南