mysql多表聯查

2021-07-24 23:34:42 字數 1960 閱讀 3915

1.交叉查詢(笛卡爾積)—基本不用

2.內連線查詢

3.外鏈結

1)左外連線

2)右外連線

4.子查詢(巢狀查詢)

eg:查詢分類名稱是手機數碼的商品

select *from product p where p.cno in(select cid from category where cname = '手機數碼');
5.分頁查詢

應用場景:

安卓和ios裡面上拉載入更多和下拉重新整理

兩種:

每次載入都查詢資料庫(適合一條資料量很大)

一次載入完,全部返回(適合一條資料量很小)

總結:主要是多練習

難點:

為student表和score表增加記錄

向student表插入記錄的insert語句如下:

insert into student values( 901,』張老大』, 『男』,1985,』計算機系』, 『北京市海淀區』);

insert into student values( 902,』張老二』, 『男』,1986,』中文系』, 『北京市昌平區』);

insert into student values( 903,』張三』, 『女』,1990,』中文系』, 『湖南省永州市』);

insert into student values( 904,』李四』, 『男』,1990,』英語系』, 『遼寧省阜新市』);

insert into student values( 905,』王五』, 『女』,1991,』英語系』, 『福建省廈門市』);

insert into student values( 906,』王六』, 『男』,1988,』計算機系』, 『湖南省衡陽市』);

向score表插入記錄的insert語句如下:

insert into score values(null,901, 『計算機』,98);

insert into score values(null,901, 『英語』, 80);

insert into score values(null,902, 『計算機』,65);

insert into score values(null,902, 『中文』,88);

insert into score values(null,903, 『中文』,95);

insert into score values(null,904, 『計算機』,70);

insert into score values(null,904, 『英語』,92);

insert into score values(null,905, 『英語』,94);

insert into score values(null,906, 『計算機』,90);

insert into score values(null,906, 『英語』,85);

mysql> select a.* from student a ,score b ,score c

-> where a.id=b.stu_id

-> and b.c_name=』計算機』

-> and a.id=c.stu_id

-> and c.c_name=』英語』;

+—–+——–+——+——-+————+————–+

| id | name | *** | birth | department | address |

+—–+——–+——+——-+————+————–+

| 901 | 張老大 | 男 | 1985 | 計算機系 | 北京市海淀區 |

| 904 | 李四 | 男 | 1990 | 英語系 | 遼寧省阜新市 |

| 906 | 王六 | 男 | 1988 | 計算機系 | 湖南省衡陽市 |

+—–+——–+——+——-+————+————–+

mysql 多表聯查 MySQL的多表聯查

今天是周二,我們一起來研究下mysql的多表聯查啊。或許你也知道,表之間的關係有 1對1 1對多 多對多。然後.1.巢狀查詢 乙個查詢的結果是另外sql查詢的條件 如 查詢stu表中年齡最大的是誰?mysql select from stu where age select max age from...

mysql多表聯查 mysql 多表聯查 例項

多表查詢 笛卡爾積查詢 笛卡爾積查詢 就是兩張表相乘,若左邊表有m條資訊,右邊表有n條資訊,那麼查詢顯示的資訊總共為m n條,這其中往往包含大量錯誤資料,需要用where 條件來過濾無用資訊 笛卡爾積查詢語句 select from dept,emp id name id name dept id ...

mysql 多表聯查

1.多表連線型別 2.1.笛卡爾積 交叉連線 在 mysql 中可以為 cross join 或者省略 cross 即 join,或 者使用 如 1.select from table1 cross join table2 2.select from table1 join table2 3.sel...