The detailed operation of the SQL statement (two)

Advanced SQL statement

  • select limit

select * from person limit 5;

Insert picture description here

  • select like

-- %可以替换多个字母
select * from person where lastname like '%b';
-- 可以两端同时匹配%
select * from person where lastname like '%o%';
-- 一个_只能匹配一个字母
select * from person where lastname like 'T_m';
-- 可以使用多个_
select * from person where lastname like 'G__es';

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

  • select charlist

-- regexp支持正则表达式
select * from person where lastname regexp '^[TB]';

Insert picture description here

  • select in

select * from person where lastname in ('Bob', 'Dell');

Insert picture description here

  • select between

select * from person where id between 1 and 5;

Insert picture description here

  • select alias

select firstname one, lastname two from person;

Insert picture description here

  • select join

Insert picture description here

Insert picture description here
Insert picture description here

  1. inner join

Insert picture description here

select Websites.name, access_log.count, access_log.date
from Websites
inner join access_log
on Websites.id=access_log.site_id
order by access_log.count;

Insert picture description here

  1. left join

Insert picture description here

select Websites.name, access_log.count, access_log.date
from Websites
left join access_log
on Websites.id=access_log.site_id
order by access_log.count desc;

Insert picture description here

  1. right join

Insert picture description here

select websites.name, access_log.count, access_log.date
from websites
right join access_log
on access_log.site_id=websites.id
order by access_log.count desc;

Insert picture description here

  1. full join

Insert picture description here

-- MySQL中不支持 full outer join,你可以在 SQL Server 测试以下实例。
select Websites.name, access_log.count, access_log.date
from Websites
full outer join access_log
on Websites.id=access_log.site_id
order by access_log.count desc;
  • select union

    The UNION operator is used to combine the result sets of two or more SELECT statements.

    Please note that each SELECT statement within UNION must have the same number of columns. The columns must also have similar data types. At the same time, the order of the columns in each SELECT statement must be the same.
    Insert picture description here
    Insert picture description here

select country from Websites
union
select country from apps
order by country;

select country from Websites
union all
select country from apps
order by country;

Insert picture description here
Insert picture description here

  • The select into
    select into statement copies data from one table and then inserts the data into another new table.
-- MySQL 数据库不支持 SELECT ... INTO 语句,但支持 INSERT INTO ... SELECT 。
select *
into newtable [in externaldb]
from table1;

select column_name(s)
into newtable [in externaldb]
from table1;
  • The insert into select
    insert into select statement copies data from a table, and then inserts the data into an existing table. Any existing rows in the target table will not be affected.
insert into Websites (name, country)
select app_name, country from apps;

insert into Websites (name, country)
select app_name, country from apps
where id=1;
  • create constraint
    Insert picture description here
create tablePersons (
    ID int not null,
    LastName varchar(255) not null,
    FirstName varchar(255) not null,
    Age int
);

create table Persons
(
P_Id int not null,
LastName varchar(255) not null,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
unique (P_Id)
);

create table Persons
(
P_Id int not null,
LastName varchar(255) not null,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
primary key (P_Id)
);

create table Persons
(
P_Id int not null,
LastName varchar(255) not null,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
check (P_Id>0)
);

create table Persons
(
    P_Id int not null,
    LastName varchar(255) not null,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) DEFAULT 'Sandnes'
);
  • auto increment
create table Persons
(
ID int not null auto_increment,
LastName varchar(255) not null,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
primary key (ID)
);
  • null
-- 无法使用比较运算符来测试 NULL 值,比如 =、< 或 <>。
-- 我们必须使用 IS NULL 和 IS NOT NULL 操作符。
select LastName,FirstName,Address from Persons
where Address is null;
  • ifnull()
select lastname, ifnull(city, '123') from person;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42647711/article/details/109233108