mysql多表查詢例子

來源:互聯網
上載者:User

標籤:size   多表   sql   0.00   sheng   base   values   ase   ble   

【注】多表查詢和串連查詢(left join等) 功能類似,沒有太大區別。

 

create database test charset utf8 collate utf8_bin;
use test;
create table tb_chengji(
xuesheng varchar(10) not null default ‘‘ primary key,
yuwen int(10)
)charset=utf8;

insert into tb_chengji values(‘亨德森‘,90),(‘拉拉納‘,100),(‘馬內‘,115),(‘庫蒂尼奧‘,99);

create table tb_info(
id int(10) not null primary key auto_increment,
xuesheng varchar(10) not null default ‘‘,
xingbie char(1) not null default ‘‘,
dizhi varchar(10)select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.xuesheng=tb_info.xuesheng and tb_chengji.yuwen>=100; not null default ‘‘
)charset=utf8;

insert into tb_info values(1,‘馬內‘,‘男‘,‘邊鋒‘),(2,‘庫蒂尼奧‘,‘男‘,‘左中場‘),(3,‘拉拉納‘,‘女‘,‘中場‘),(4,‘亨德森‘,‘女‘,‘後腰‘);


多表查詢例子:
例子1      

select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.xuesheng=tb_info.xuesheng and tb_chengji.yuwen>=100;
+----+-----------+---------+--------+-------+
| id | xuesheng | xingbie | dizhi | yuwen |
+----+-----------+---------+--------+-------+
| 1 | 馬內 | 男 | 邊鋒 | 115 |
| 3 | 拉拉納 | 女 | 中場 | 100 |
+----+-----------+---------+--------+-------+
2 rows in set (0.08 sec)


例子2          

select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.yuwen>=100;
+----+-----------+---------+-----------+-------+
| id | xuesheng | xingbie | dizhi | yuwen |
+----+-----------+---------+-----------+-------+
| 1 | 拉拉納 | 男 | 邊鋒 | 100 |
| 1 | 馬內 | 男 | 邊鋒 | 115 |
| 2 | 拉拉納 | 男 | 左中場 | 100 |
| 2 | 馬內 | 男 | 左中場 | 115 |
| 3 | 拉拉納 | 女 | 中場 | 100 |
| 3 | 馬內 | 女 | 中場 | 115 |
| 4 | 拉拉納 | 女 | 後腰 | 100 |
| 4 | 馬內 | 女 | 後腰 | 115 |
+----+-----------+---------+-----------+-------+
8 rows in set (0.00 sec)


例1                tb_chengji.xuesheng=tb_info.xuesheng將表tb_chengji和tb_info串連起來,叫做等同串連。   

例2     如果不使用tb_chengji.xuesheng=tb_info.xuesheng,那麼產生的結果將是兩個表的笛卡爾積,叫做全串連。

mysql多表查詢例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.