Advanced SQL statement
select * from person limit 5;
-- %可以替换多个字母
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';
-- regexp支持正则表达式
select * from person where lastname regexp '^[TB]';
select * from person where lastname in ('Bob', 'Dell');
select * from person where id between 1 and 5;
select firstname one, lastname two from person;
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;
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;
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;
-- 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.
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;
- 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
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;