create
table
`customer` (
`id`
int(11) not
null auto_increment,
`lastname`
char(50) default
null,
`firstname`
char(50) default
null,
`address`
char(50) default
null,
`city`
char(50) default
null,
`county`
char(50) default
null,
`phone`
char(20) default
null,
primary
key (`id`)
);
create
table
`supplier` (
`id`
int(11) not
null auto_increment,
`companyname`
char(50) default
null,
`contactname`
char(50) default
null,
`city`
char(50) default
null,
`county`
char(50) default
null,
`phone`
char(20) default
null,
`fax`
char(20) default
null,
primary
key (`id`)
);
create
table
`orders` (
`id`
int(11) not
null auto_increment,
`orderdate` datetime default
null,
`ordernumber`
char(30) not
null,
`customerid`
int(11) not
null,
`totalamount`
int(11) not
null,
primary
key (`id`)
);
create
table
`orderitem` (
`id`
int(11) not
null auto_increment,
`orderid`
int(11) not
null,
`productid`
int(11) not
null,
`unitprice`
float
notnull,
`quantity`
int(11) not
null,
primary
key (`id`)
);
create
table
`product2` (
`id`
int(11) not
null auto_increment,
`productname`
char(100) not
null,
`supplierid`
int(11) not
null,
`unitprice`
float
notnull,
`package`
int(11) not
null,
`isdiscontinued` tinyint(4) not
null comment '是否下架',
primary
key (`id`)
);
select
o.ordernumber as 訂單號,
o.orderdate as 訂購日期,
p.productname as 物品名稱,
i.quantity as 購買數量,
i.unitprice as 單價
from
orders o
join orderitem i on o.id = i.orderid -- join 即 inner
join
join product p on p.id = i.productid
order
by o.ordernumber
例如,列出所有人的訂單資訊
select
c.firstname,
c.lastname,
c.city,
c.country,
o.ordernumber,
o.totalamount
from
customer c -- customer表中的每乙個人
left
join orders o -- orders就是每乙個人對應的訂單資訊
on o.customerid = c.id -- customer 與 orders的 聯絡
order
by -- 排序
totalamount
任何right join都可以換成left join的寫法,反之亦然。
例如,下面兩種寫法的查詢結果是等價的:
-- left join
select
totalamount,
firstname,
lastname,
city,
country
from
customer c
left
join orders o on o.customerid = c.id
where
totalamount is
null;
-- right join
select
totalamount,
firstname,
lastname,
city,
country
from
orders o
right
join customer c on o.customerid = c.id
where
totalamount is
null;
例如,如何查詢各個國家的所有客戶和**商?
分析:
1. 查詢各個國家,需要按國家排序
2. 查詢客戶和**商,需要從customer表和supplier表中查詢;
3. 查詢所有客戶和所有**商,需要使用full join;
select c.firstname, c.lastname, c.country as customercountry, s.country as suppliercountry, s.companyname from customer c full
join supplier s on c.country = s.country order
by c.country, s.country;
如果需要從兩個表中查詢指定的字段資訊,可以使用join;
如果同時查詢所有客戶對應的**商資訊,可以用from customer left join supplier;
如果同時查詢所有**商對應的客戶資訊,可以用from customer right join supplier;
如果同時查詢所有客戶和所有**商的資訊,可以用from customer full join suppliersupplier;
有些資料庫不支援full join,例如mysql,不過在mysql中可以使用left join + union + right join 實現類似full join的功能,例如上述例子如果在 mysql中可以這樣寫:
select c.firstname, c.lastname, c.country as customercountry, s.country as suppliercountry, s.companyname from customer c left
join supplier s on c.country = s.country
union
select c.firstname, c.lastname, c.country as customercountry, s.country as suppliercountry, s.companyname from customer c right
join supplier s on c.country = s.country order
by suppliercountry,customercountry;
sql語句總結
sql語句 分為兩大類 針對資料庫database和表table的操作 建立create 刪除drop 修改alter 檢視show 建立乙個資料庫 create database mydb 刪除表drop table users 針對表中的記錄 資料 的操作 增insert語句 刪delete語句...
SQL語句總結
dml資料操作語言 create db 建立資料庫 語法 create database database name 實際 建立乙個名為my db的資料庫 create database my db create table 建立資料庫中表 建立新錶 語法 create table 表名稱 列名稱1...
SQL語句總結
前言 本文總結下sql語句,舉個例子 create table sp email id int 11 not null auto increment,from id int 11 not null comment 傳送者id to id int 11 not null comment 接收者id t...