SQL- SQL query to retrieve a stage

A description

If you are a beginner, it is recommended to install Internet to find articles Mysql installation, and the use of navicat connect to the database, after the example of the basic is to use the mysql database management system;

Two preparation premise

The need to establish a student tables, columns are id, name, age, student information; this example, the article space reasons SQL comment a little;

Construction of the table statement:

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Data in the table:

INSERT INTO `springboot`.`student`(`id`, `name`, `age`, `info`) VALUES (1, 'youku1', 18, '大一新生');
INSERT INTO `springboot`.`student`(`id`, `name`, `age`, `info`) VALUES (2, 'youku2', 23, '毕业生');
INSERT INTO `springboot`.`student`(`id`, `name`, `age`, `info`) VALUES (3, 'jeff', 25, '社会人士');
INSERT INTO `springboot`.`student`(`id`, `name`, `age`, `info`) VALUES (4, 'smile', 17, '高三学子');

Three Basic Search

3.1 select keywords

sql statements are made a number of keywords (keyword) consisting of keywords is a reserved word database, the user can not be built as a table name table, fields, etc.; data table retrieved using select keyword query as the beginning information database tables;

Retrieving a single field 3.2

Statement example:

SELECT name FROM student 

search result:

youku1
youku2
jeff
smile

Statement Analysis:

is meant select query, the field name in the table represents, from which tables indicates from which table is behind the student; means to link the data field name is to query the student from the table;

3.3 sql statement Precautions

Execute multiple sql statements together with the use of titles (;) separated keyword spelling statement tables are recommended to use upper case, the table field and table name in lowercase; for easy reading suggestions to sql multiple lines (due to the length of the article reason regardless of); sql language is English letters, Chinese do not open, so as to avoid a symbol error is not easy to find; sql statement default case-insensitive;

3.4 retrieve multiple fields

Statement example:

SELECT name,age FROM student;

Statement results:

youku1  18
youku2  23
jeff    25
smile   17

Statement Analysis:

Query field name, age, from the table student; use commas to retrieve multiple columns (,) separated;

3.5 to retrieve all fields

Statement example:

SELECT * FROM student;

Statement results:

1   youku1  18  大一新生
2   youku2  23  毕业生
3   jeff    25  社会人士
4   smile   17  高三学子

Statement Analysis:

Wildcard (*) to return all columns in the table, if not necessarily recommend not using wildcards, will affect database performance;

3.6 distinct

represents a distinct distinction is meant only retrieved rows (de-emphasis) which on top of the column; If the keyword distinct, which acts on the back of all the columns (data not repeated since the present exemplary example, the result not very clear);

SELECT DISTINCT name, age FROM student;

Statement results:

youku1  18
youku2  23
jeff    25
smile   17

3.7 limit the number of

access and sql server:

SELECT TOP 2 name FROM student

2 represents a front limit the TOP return line 2

postgresql 、SQLite和 mysql:

SELECT name FROM student LIMIT 2;

2 represents the limit line 2 before returning limit;

DB2:

SELECT name FROM student FETCH FIRST 2 ROWS ONLY

FETCH FIRST 2 ROWS ONLY represents only fetch data Pre 2

Statement results:

youku1
youku2

3.8 Offset

Example:

SELECT name FROM student LIMIT 1 OFFSET 1;

Statement Analysis

It indicates that the query from the student table column name limit the Number 1, the offset value 1; second means is to query the student data table row; represents offset or shift the jump;

mysql and MariaDB simplified form:

SELECT name FROM student LIMIT 1,2;

Indicates that the query field name from the list of students, two limits, an offset; attention to the order;

Statement results:

youku2
jeff

Four sorting retrieval

4.1 ORDER BY clause

Example:

SELECT name,age FROM student ORDER BY age

Statement analysis;

Search field name, age students from the age of tables sorted according to the column; Note that the default is ascending, ORDER BY clause is usually the end of the sentence;

Statement results:

smile   17
youku1  18
youku2  23
jeff    25

More than 4.2 column sort

Example:

SELECT name,age FROM student ORDER BY age DESC, name ASC;

Statement Analysis:

Query name, age students from the table, in descending order according to age, name in ascending order; keyword DESC (descending) means descending order, the letter default ZA; ASC (ascending) means ascending order, default letters AZ; multi-columns, each columns specify DESC later, a comma (,) separated, if not written, the default ascending;

Statement results:

jeff    25
youku2  23
youku1  18
smile   17

4.3 Sort by bit

Statement example:

SELECT name,age FROM student ORDER BY 2 DESC, 1 ASC;

Refers to the query field bit position, corresponding to 2 fields age, 1 corresponding to the field name; consistent results and 4.2;

Five Filters retrieval

sql statement filter conditions (filter condition) keyword is WHERE, its use is following the table name;

5.1 WHERE operator Statement

Depending on the database management system that supports the operator is slightly different, so many repeat operator meaning below, should refer to the official documentation, which is the operator supports the use of database management systems;

Operators Explanation
= equal
> more than the
< Less than
!= not equal to
<> not equal to
>= greater or equal to
<= Less than or equal
!< not less than
!> no greater than
BETWEEN in the middle
IS NULL Is empty

5.2 single filter condition

Example:

SELECT * FROM student WHERE name = 'jeff';

Statement analysis;

All fields inquiries from students that the student table name is jeff; note that non-table field, table names, keywords, use two single quotes ( '') enclosed, which is stored in our data input;

Statement results;

3   jeff    25  社会人士

More than 5.3 filter conditions

When multiple filter conditions using AND or OR clause; AND connection expression represents the filter conditions are true transactions; OR connection expression matching it represents any one of;

AND Example:

SELECT * FROM student WHERE age >= '18' AND age <= '23';

Statement Analysis:

Search fields from students table, with the proviso that not less than 18 and the age of the students students younger than 23;

Statement results:

1   youku1  18  大一新生
2   youku2  23  毕业生

OR Example:

SELECT * FROM student WHERE age >= '18' OR age <= '23';

Statement Analysis:

Retrieve all fields from Table students, student older than that equal to 18, less than 23 or the age of the students;

OR and AND Example:

SELECT * FROM student WHERE age >= '18' AND (age <= '23' OR id >=2);

Statement Analysis:

When using OR and AND should clear the filter criteria, and then use brackets, because the database management system is based on the execution order, if you do not use parentheses likely to result in semantic errors; query all the fields from the student table filter criteria were older than 18 and the data (id younger than 23 or greater than 2);

Range 5.4 Queries

Example:

SELECT * FROM student WHERE  age BETWEEN '18' And '23';

Statement Analysis

BETWEEN represents the range query, the query all the fields from the students table, filtering criteria students between the ages of 18-23;

Statement results:

1   youku1  18  大一新生
2   youku2  23  毕业生

5.5 null query

Example:

SELECT * FROM student WHERE  age IS NULL;

Statement Analysis:

All fields inquiries from students table, the filter condition age of the students is empty; the database table is not populated with data by default is empty (NULL), of course, you can set the default value to the specified type of column;

5.6 IN operation

Example:

SELECT * FROM student WHERE  age IN (18,23,25);

Statement Analysis:

Search fields from students table, filters, age 18 or 23 or 25; IN a range query, matching any value specified in parentheses, with the function OR Similarly, a plurality of OR IN is equivalent to good;

Statement results:

1   youku1  18  大一新生
2   youku2  23  毕业生
3   jeff    25  社会人士

5.7 NOT operator

Example:

SELECT * FROM student WHERE  NOT age='25';

Statement Analysis:

NOT operator table is negative; its back with similar functions in the WHERE <>;

Six wildcard retrieval

6.1 Introduction wildcard

Wildcards are special strings match pattern; if the students used the regular expression is certainly no stranger to this analogous thereto; wildcards to retrieve the text is used in the back Keywords Like;

6.2 Tsuhaifu%

Example:

SELECT * FROM student WHERE name LIKE 'you%' ;

Statement Analysis:

All fields inquiries from students table, filtering criteria to match the name behind you at the beginning of a string matches any number of any character; on behalf of any number of wildcards% any string, 0 is also taken into account, but does not include null;

Statement results:

1   youku1  18  大一新生
2   youku2  23  毕业生

Example:

SELECT * FROM student WHERE name LIKE '%i%' ;

Statement Analysis:

Search table columns from the student, the student's name matches the filter conditions must occur exactly once among the letters i, i is the letter before and after the match any arbitrary character string;

Statement results;

4   smile   17  高三学子

6.3 Tsuhaifu _

_ On behalf of a wildcard match a string; in the Access database but not _?;

Example:

SELECT * FROM student WHERE name LIKE 'youku_' ;

Statement Analysis:

Search table columns from the student, the student names match the filter conditions youku behind a pattern of arbitrary string;

Statement results;

1   youku1  18  大一新生
2   youku2  23  毕业生

6.4 Tsuhaifu []

Wildcard [] represents a position of a character match the specified; which a plurality of characters which can be stored, a relationship or, only occupies a position matching pattern, Access, SQL SERVER support;

Seven basic operation field

7.1 field splicing

Example:

SELECT concat('你好啊',name,'327今天心情怎么样') FROM student WHERE id = '1' ;

Statement Analysis:

Concat function is to splice multiple characters substring into a string; the way it uses different database management systems vary slightly, should refer to the official documents;
use concat function in mysql; || use in postgresql in; and in Access + sql server used;

Statement results:

你好啊youku1327今天心情怎么样

7.2 remove the empty string

Statement example:

SELECT  RTRIM('      哥,今天管饱        ') FROM student WHERE id = '1' ;

Statement Analysis:

RTRIM (STR) function is to remove the string on the right; TRIM (STR) is removed on both sides of the string of blank characters; LTRIM (STR) is to remove the left of the string of blank characters;

Statement results;

      哥,今天管饱

7.3 alias

Statement example:

SELECT name as student_name FROM student WHERE  id = '1' ;

Statement Analysis:

Alias ​​(Alias) is a field, or aliases table; use an alias table when the multi-duplicated field operation is a good choice; aliases can use the AS keyword, although it is omitted, but usually we add best it enhances readability;

7.4 computing

Operators Explanation
* Multiply
+ plus
- Less
/ except

Statement example:

SELECT 2 * 8; 

Statement results:

16

Eight About the Author

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zszxz/p/12059163.html