SQLステートメントの詳細な操作(2つ)

高度なSQLステートメント

  • 制限を選択

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';

ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入

ここに画像の説明を挿入

  • charlistを選択します

-- 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;

ここに画像の説明を挿入

  • 参加を選択

ここに画像の説明を挿入

ここに画像の説明を挿入
ここに画像の説明を挿入

  1. 内部結合

ここに画像の説明を挿入

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;

ここに画像の説明を挿入

  1. 左結合

ここに画像の説明を挿入

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;

ここに画像の説明を挿入

  1. 右結合

ここに画像の説明を挿入

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;

ここに画像の説明を挿入

  1. 完全結合

ここに画像の説明を挿入

-- 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;
  • ユニオンを選択

    UNION演算子は、2つ以上のSELECTステートメントの結果セットを組み合わせるために使用されます。

    UNION内の各SELECTステートメントには、同じ数の列が必要であることに注意してください。列にも同様のデータ型が必要です。同時に、各SELECTステートメントの列の順序は同じである必要があります。
    ここに画像の説明を挿入
    ここに画像の説明を挿入

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;

ここに画像の説明を挿入
ここに画像の説明を挿入

  • select into
    select intoステートメントは、あるテーブルからデータをコピーしてから、そのデータを別の新しいテーブルに挿入します。
-- MySQL 数据库不支持 SELECT ... INTO 语句,但支持 INSERT INTO ... SELECT 。
select *
into newtable [in externaldb]
from table1;

select column_name(s)
into newtable [in externaldb]
from table1;
  • insert into select
    insert into selectステートメントは、テーブルからデータをコピーしてから、既存のテーブルにデータを挿入します。ターゲットテーブルの既存の行は影響を受けません。
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 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'
);
  • 自動増加
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 值,比如 =、< 或 <>。
-- 我们必须使用 IS NULL 和 IS NOT NULL 操作符。
select LastName,FirstName,Address from Persons
where Address is null;
  • ifnull()
select lastname, ifnull(city, '123') from person;

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_42647711/article/details/109233108