SQL簡明例項教程

2021-08-02 15:18:17 字數 4675 閱讀 6797

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注入,可以利用布林和延時注入來進行判斷 例如布林的判斷是否注入成功 1 and 1 2 1 and 1 2 1 and 1 2 1 當輸入1 and 1 2顯示為假時,則說明注入成功 2 接下來判斷字串長度 利用length 函式,通過二分法來判斷 id 1 and length ...

awk例項教程

1.awk語法規則 awk 2.顯示最近登入的5個帳號 last n 5 awk root root root dmtsai root awk工作流程是這樣的 讀入有 n 換行符分割的一條記錄,然後將記錄按指定的域分隔符劃分域,填充域,0則表示所有域,1表示第乙個域,n表示第n個域。預設域分隔符是 ...

CSS reflow例項教程

frame主要的動作有三個 構造frame,以建立物件樹 dom樹 reflow,以確定物件位置,或者www.cppcns.com是呼叫mozilla的layout 這裡是指原始碼的實現 繪製,以便物件能顯示在螢幕上 總的來說,reflow就是載入內容樹 在html中就是dom樹 和建立或更新fra...