mysql連線查詢例項 MySQL連線查詢例項詳解

2021-10-18 21:20:19 字數 3344 閱讀 6343

建立表suppliers:

create table suppliers

s_id int not null auto_increment,

s_name char(50) not null,

s_city char(50) null,

s_zip char(10) null,

s_call char(50) not null,

primary key (s_id)

insert into suppliers(s_id, s_name,s_city, s_zip, s_call)

values(101,'fastfruit inc.','tianjin','300000','48075'),

(102,'lt supplies','chongqing','400000','44333'),

(103,'acme','shanghai','200000','90046'),

(104,'fnk inc.','zhongshan','528437','11111'),

(105,'good set','taiyuang','030000', '22222'),

(106,'just eat ours','beijing','010', '45678'),

(107,'dk inc.','zhengzhou','450000', '33332');

內連線select suppliers.s_id, s_name,f_name, f_price

from fruits ,suppliers

where fruits.s_id = suppliers.s_id;

使用 inner join 語法進行內連線查詢

select suppliers.s_id, s_name,f_name, f_price

from fruits

inner join suppliers on fruits.s_id = suppliers.s_id;

建立表orders:

create table orders

o_num int not null auto_increment,

o_date datetime not null,

c_id int not null,

primary key (o_num)

insert into orders(o_num, o_date, c_id)

values(30001, '2008-09-01', 10001),

(30002, '2008-09-12', 10003),

(30003, '2008-09-30', 10004),

(30004, '2008-10-03', 10005),

(30005, '2008-10-08', 10001);

左連線select customers.c_id, orders.o_num

from customers left outer join orders

on customers.c_id = orders.c_id;

右連線select customers.c_id, orders.o_num

from customers right outer join orders

on customers.c_id = orders.c_id;

復合條件連線查詢

select customers.c_id, orders.o_num

from customers inner join orders

on customers.c_id = orders.c_id and customers.c_id = 10001;

select suppliers.s_id, s_name,f_name, f_price

from fruits inner join suppliers

on fruits.s_id = suppliers.s_id

order by fruits.s_id;

【例.46】在fruits表和suppliers表之間使用內連線查詢,查詢之前,檢視兩個表的結構

select suppliers.s_id, s_name,f_name, f_price

from fruits ,suppliers

where fruits.s_id = suppliers.s_id;

【例.47】在fruits表和suppliers表之間使用inner join語法進行內連線查詢

select suppliers.s_id, s_name,f_name, f_price

from fruits inner join suppliers

on fruits.s_id = suppliers.s_id;

【例.48】查詢**f_id='a1'的水果**商提供的其他水果種類

select f1.f_id, f1.f_name

from fruits as f1, fruits as f2

where f1.s_id = f2.s_id and f2.f_id = 'a1';

【例.49】在customers表和orders表中,查詢所有客戶,包括沒有訂單的客戶,sql語法如下

select customers.c_id, orders.o_num

from customers left outer join orders

on customers.c_id = orders.c_id;

【例.50】在customers表和orders表中,查詢所有訂單,包括沒有客戶的訂單

select customers.c_id, orders.o_num

from customers right outer join orders

on customers.c_id = orders.c_id;

【例.51】在customers表和orders表中,使用inner join語法查詢customers表中id為10001、的客戶的訂單資訊

select customers.c_id, orders.o_num

from customers inner join orders

on customers.c_id = orders.c_id and customers.c_id = 10001;

【例.52】在fruits表和suppliers表之間使用inner join語法進行內連線查詢,並對查詢結果排序

select suppliers.s_id, s_name,f_name, f_price

from fruits inner join suppliers

on fruits.s_id = suppliers.s_id

order by fruits.s_id;

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢例項 例項講解MySQL連線查詢

mysql連線查詢相信大家都有所了解,連線查詢是在資料庫查詢操作的時候肯定要用到的,下面就為您介紹mysql連線查詢的用法,供您參考學習。首先我們做兩張表 員工資訊表和部門資訊表,在此,表的建立只為講述連線的概念,所以字段非常的簡單 employeetb 員工資訊表 employeeid emplo...

MySQL高階查詢 連線查詢例項

使用sql查詢很簡單,很基礎的sqlect語句查詢。如果想從多個表查詢比較複雜的資訊,就會使用高階查詢實現。常見的高階查詢包括多連線查詢,外連線查詢與組合查詢等,今天我先學習最常用的連線查詢。我先以一張person表 id,name,phone,age 與乙個student表 id,name,qq,...