Query Fundamentals --SELECT, operators

SELECT statement basis

Queries column

SELECT clause of the query include a desired table from the name of the column, and the FROM clause
specifies the data table to select a name.

#查询特定的列
SELECT < 列名 > ,……
 FROM < 表名 > ;
#查询全部的列
SELECT   *
FROM < 表名 >;

Next, we try to create from Chapter 1 Product (commodity) table, the query
out product_id (Product Number) column shown in Figure 2-1, product_name (quotient
goods name) column and purchase_price (purchase price) column
Here Insert Picture Description

SELECT product_id,product_name,purchase_price FROM product;

Check out the order of the columns can be arbitrary. Query multiple columns, use a comma to separate them. Columns in order and the query results in the same order as the SELECT clause.

Set an alias for the column

SQL statement can use the AS keyword set an alias for the column

SELECT product_id AS id,
product_name AS name,
purchase_price AS price
FROM Product;
#别名可以使用中文,使用中文时需要用双引号(")括起来
SELECT product_id AS " 商品编号 ",
product_name AS " 商品名称 ",
purchase_price AS " 进货单价 "
FROM Product;

Constant queries

SELECT clause can not only write the column names, you can also write constants. Listing 2-6
in the SELECT clause of the first column of 'Product' is a string constant, the second column 38 is a digital
constant, the 3 '2009-02-24' date is a constant, and they _ Product id
columns and columns being queried product _ name out together.

SELECT ' 商品 ' AS string, 38 AS number, '2009-02-24' AS date,
product_id, product_name
FROM Product;

Here Insert Picture Description

Remove duplicate rows from the results

Want to delete duplicate rows, you can use DISTINCT in the SELECT clause to achieve
Here Insert Picture Description

SELECT DISTINCT product_type
FROM Product;

When using DISTINCT, NULL is also considered a type of data. NULL is present in plurality
when the lines will be combined into a NULL data. Purchase of NULL data containing _
. Price (purchase price) column using a SELECT DISTINCT statement. In addition to the two 2800 data, two NULL data is also merged into one.

DISTINCT may be used before as multiple columns like Listing 2-9. In this case, it will be
the data of a plurality of columns be combined to be combined into one duplicate data.

SELECT DISTINCT product_type, regist_date
FROM Product;

Here Insert Picture Description

where statement to select records

The previous examples are all selected data will be stored in the table, but the reality is not every
time out will need to select all the data, most cases are chosen to meet the "types of goods as clothing
dress," "sales price in 1000 data certain conditions yen or more "and so on.

SELECT < 列名 >, ……
FROM < 表名 >
WHERE < 条件表达式 >;

First recorded by the WHERE clause of a query meet the specified criteria, and then select the columns specified in the SELECT statement.
SQL clause in writing order is fixed and can not be altered. WHERE clause must
after the FROM clause, followed by written order of occurrence, then change will result in execution error

SELECT product_name, product_type
FROM Product
WHERE product_type = ' 衣服 ';

Here Insert Picture Description

Notes wording

1 line comments
write "-", the only written on the same line.
Multi-line comments
written in the "/ " and " between /" that can span multiple lines.

Operators

Signed arithmetic operator is performed (Comparative calculation or size) column or on both sides of its value.

Arithmetic Operators

SELECT product_name, sale_price,
sale_price * 2 AS "sale_price_x2"
FROM Product;

Here Insert Picture Description
As used four arithmetic operators (+, -, *, /) is called arithmetic operators. Operator to
use both sides of the value of its four operations or string concatenation, the size of the value comparison, etc.,
and returns the sign of the result. Addition operator (+) before and after the case of digital or digital type of column
names, it will return the result of addition. SQL in addition to its arithmetic operators as well as
his various operators. You can also use SQL as parentheses () operation expression as usual
all calculations contain NULL, the result is NULL.

Comparison Operators

Learning WHERE clause, we use the symbol = selected from the Product table
out the types of goods (product _ type) to record 'clothes' string. Let
us use the symbol = and then select the sales price (sale_price) to 500 yen (digital
500) records

SELECT product_name, product_type
FROM Product
WHERE sale_price = 500;

Such as symbol = symbol for comparing the value of the column or both sides is referred to as comparison operator,
the symbol = is a comparison operator. In the WHERE clause can use comparison by combining
a variety of conditional expression
Next, we use "not equal to" negative connotation representative of such comparison operators <>,
the value of the selected row is not sale _ price record 500

SELECT product_name, product_type
FROM Product
WHERE sale_price <> 500;

Here Insert Picture Description
Select the sales price than or equal to 1,000 yen recording

SELECT product_name, product_type, sale_price
FROM Product
WHERE sale_price >= 1000;

Select the record date of registration before September 27, 2009 of

SELECT product_name, product_type, regist_date
FROM Product
WHERE regist_date < '2009-09-27';

Comparison operators may also be used to compare the results. In the WHERE clause specifies the sales price (sale_price) than the stock unit price (purchase_price) above 500 yen conditional expression higher. In order to determine whether or not higher than 500 yen, need purchase_price sale_price column value minus
the value of the column.
In the WHERE clause of the conditional expression may be calculated using the expression

SELECT product_name, sale_price, purchase_price
FROM Product
WHERE sale_price - purchase_price >= 500;

The notes on the strings using inequality

创建 Chars 表并插入数据
-- DDL :创建表
CREATE TABLE Chars
(chr CHAR(3) NOT NULL,
PRIMARY KEY (chr));
SQL Server PostgreSQL
-- DML :插入数据
BEGIN TRANSACTION;
INSERT INTO Chars VALUES ('1');
INSERT INTO Chars VALUES ('2');
INSERT INTO Chars VALUES ('3');
INSERT INTO Chars VALUES ('10');
INSERT INTO Chars VALUES ('11');
INSERT INTO Chars VALUES ('222');
COMMIT;

Select a SELECT statement is greater than '2' data

SELECT chr
FROM Chars
WHERE chr > '2';

Here Insert Picture Description
chr column is defined as a string type, and when the string data type size comparison, and the digital comparator using different rules. A typical rule is carried out in accordance with the dictionary order comparison, that is, as the name that, in accordance with the order of the entries appear in the dictionary to sort. The most important rule is that words that begin with the same character more similar than different words that begin with the characters.
Results Table Chars column chr lexicographically sorted as follows.
. 1
10
. 11
2
222
. 3

The selection of a NULL

= NULL NULL with operator can not make
a recording of the selected NULL

SELECT product_name, purchase_price
FROM Product
WHERE purchase_price IS NULL;

Conversely, it is desirable to select the recording is not NULL, requires the use of IS NOT NULL transport
operator

SELECT product_name, purchase_price
FROM Product
WHERE purchase_price IS NOT NULL;
Released six original articles · won praise 8 · views 1202

Guess you like

Origin blog.csdn.net/weixin_43289424/article/details/103899430