当前位置:网站首页>SQL basic query

SQL basic query

2022-06-11 08:52:00 Lizexin

Basic query method :
select   Field    from   Table name ;
select * from  Table name ;
duplicate removal
select distinct  Field   from   Table name ;
Basic condition query
select  Field   from   Table name   where    Conditions    Operator    value ;
Operator name
= be equal to
<> It's not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
BETWEEN In a certain range
LIKE contain
 notes :
 In some versions of sql in , The operator <> Can be written as !=.
sql Use single quotation marks in to surround the text value ( Most database systems also accept double quotes ), Values don't use quotation marks .
and and or Operator
and: If the first condition and the second condition hold , be and Operator to display a record .
or: If only one of the first condition and the second condition holds , be or Operator to display a record .
select  Field  from  Table name   where   Field   Operator   value    and/or    Conditions    Operator    value ;

 notes :and And or combination ( Use parentheses to form complex expressions ) 
select  Field   from   Table name   where   ( Conditions    Operator     value    and/or    Conditions     Operator    value )  and/or   Conditions     Operator    value ;
order   by Default sort 
desc    In reverse order 
select    Field   from   Table name    order  by or desc;
Insert
insert  into    Table name    values  ( value 1, value 2,...);
insert into  Table name ( Field 1, Field 2) voalues ( value 1, value 2,...;
modify
update   Table name    set   Field  =  The new value   where    Field  =  Certain value ;
Delete
drop  Delete table : Delete content and definition , Release space 
drop table  Table name ;

delete   Delete content , Do not delete table structure , Delete line by line 
delete table  Table name ;
delete   from    Table name   where   Conditions  =  value ;

truncate  Delete content 、 Release space , But don't delete the table structure ;
truncate table  Table name ;
like Fuzzy query
select    Field   from   Table name     where    Field  like   'N%';
 notes :“%” Can be used for the first wildcard 
 wildcard   describe 
%  Replace one or more characters 
_  Replace only one character 
[charlist]  Any single character in the character column 
[^charlist] perhaps [!charlist]  Any single character not in the character column 
in
select     Field    from    Table name   where    Field   in  (value1,value2,...;
between…and Select a data range between two values , These values can be numerical 、 Text or date .
select    Field   from   Table name   where   Field    between   value1    and   value2;
Alias Alias
 The table alias :
select    Field   from    Table name    as    name ;
 The alias of the field :
select    Field    as   name   from    Table name ;
join

join The field contents and values are consistent
inner join Internal connection : The result is the intersection of the two sides

select  Field    from  Table name 1  inner  jion   Table name 2  on   Table name 1. Field = Table name 2. Field 

left join Left connection : Based on the table on the left , Whether the conditions are met or not, all the data on the left will be displayed , The data on the right shows only those that can be matched , Use when it doesn't match null fill ;

select    Field   from   Table name 1 left   jion   Table name 2   on   Table name 1. Field = Table name 2. Field 

right join The right connection : The way of understanding is the same as the left connection , It's just that the table on the right is used as the benchmark , To match the data on the left , Only those that can be matched are displayed , No match null fill ;

select   Field   from   Table name 1  right jion   Table name 2   on    Table name 1. Field = Table name 2. Field 

FULL OUTER JOIN: Full connection , The result is a table A And table B Union , That is to say, it will all be displayed

SELECT  Field 
FROM  surface 1
FULL OUTER JOIN  surface 
ON  surface 1. Field = surface 2. Field ;

Cross join: Cross connect

SELECT  Field  FROM  surface 1 CROSS JOIN  surface 2 WHERE  Conditions ;
 or 
SELECT  Field  FROM  surface 1,  surface 2 WHERE  Conditions ;

union Inside select Statements must have the same number of columns .
Columns must be of the same type , At the same time, the column name order must be the same .
Different types can be cast cast ( Field as type :int float double varchar char etc. )

select    Field   from   Table name 1
union
select   Field  from    Table name 2
 notes : Default union Operator selects different values , If duplicate values are allowed , You can use union  all.
select into Add temporary table
select   into   Statement selects data from a table , Then insert the data into another temporary table .
select   *   into   A temporary table   from   old surface ;
select  *  into    A temporary table   from   old surface   where    Conditions     Operator     value ;
select count()
count(*) Function returns the number of record rows in the table :
select  count(*)  from    Table name ;
select sum ()
sum  Function returns the total number of columns of numbers ( Total ).
select   sum( Field )  from    Table name ;
 notes  : Usually cooperate group  by Use ;
group by
group  by    Used to combine the total function , Group result sets according to one or more columns ;
select    Field 1,sum( Field 2)   from    Table name     group by   Field 1;
原网站

版权声明
本文为[Lizexin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110843048759.html