DQL single-table queries

DQL
data query language
data query keyword: select
data in the database to query relational tables

Create a database
to create a table
Student table (Student ID s_no, name s_name, class s_classno, courses s_courseno)

Class table (number of classes c_no, class name c_name, class location c_d)

Curriculum (course number cou_no, course name cou_name, course credit cou_score)


1. Query table all the fields in
select * from table name;

2, the query specified field
select an attribute name, attribute name 2, ..., n from property name table;
condition inquiry

3 records, related query criteria specified field
select an attribute name, attribute name 2, ..., n from property name table where expression;
expression format of the where clause is: name attribute operator value.
where the expression, there may be <,>, =, <> ,! =,> =, <=, [not] between and, like ( wildcard% _ fuzzy match)
% match any number of characters
_ matches any character

like "Zhang%" match: Joe Smith, Zhang longbow, Zhang Wuji ......
like "Zhang _" matches: Joe Smith, Zhang two, Zhang

like "% Zhang _" matches: one thousand two hundred thirty-four four
can not match Sally;

4, a plurality of matching queries
where Expression 1 and Expression 2;
where Expression 1 or Expression 2; or may be used in place of ||
select * from student where s_no = 1 or s_name = ' John Doe';

5, the query in the collection
in
the SELECT * from Student S_NO in the WHERE (1,6,3);


6, outside the set of a query
not in
the SELECT * from Student S_NO not in the WHERE (2, 4);

7, the query null
IS null
the SELECT * from class s_birchday the WHERE IS null;

8, non-null value query
keyword: is not null (not null IS not)
the SELECT * from class s_birchday the WHERE IS not null;

9, to re-query the data
DISTINCT
the SELECT DISTINCT s_classno from Student;
Note: distinct can only attribute to a heavy


10, paging query
limit [n-,] m
n-offset is, only the number of m-tuples
beginning from the 1 + n pieces of data, each of the data read back

11, combined query result set
union or union all
union joint at the same time to re-
union all union all

Result set and a temporary table: select data is not displayed directly from the table, but something is displayed in the result is a virtual table we look the same

12, the data table copied
insert into table select 1 * from TABLE 2

13, to the table by querying the existing value inserted
insert into stu1 select s_no, s_name from student;

14, SELECT INTO
(in whole or in part), and then inserting the data from one table to another new selection table.
select * into newstudent from student;

15, assigned to the new table by querying
create table stu2 (select * from student );
only copy, and further need to copy the primary key

16, alias
as (as an alternative may be omitted by spaces)

17, the sort
order by attribute name (ascending)
order by desc attribute name (ascending)

 

Guess you like

Origin www.cnblogs.com/liugangjiayou/p/11710466.html