MySQL 連線查詢

2021-07-25 15:51:49 字數 3324 閱讀 9423

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;

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連線查詢例項詳解

建立表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連線查詢on 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...