㈠ sql里3個表的連接查詢的語句怎麼寫呀
select * from 表1,表2,表3 where 表1.欄位=表2.欄位 and 表1.欄位=表3.欄位。
結構化查詢語言(Structured Query Language)簡稱SQL,是一種特殊目的的編程語言,是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統。
一、簡介
(1)SQL語言1974年由Boyce和Chamberlin提出,並首先在IBM公司研製的關系資料庫系統SystemR上實現。由於它具有功能豐富、使用方便靈活、語言簡潔易學等突出的優點,深受計算機工業界和計算機用戶的歡迎。
(2)1980年10月,經美國國家標准局(ANSI)的資料庫委員會X3H2批准,將SQL作為關系資料庫語言的美國標准,同年公布了標准SQL,此後不久,國際標准化組織(ISO)也作出了同樣的決定。
SQL從功能上可以分為數據定義、數據操縱和數據控制。SQL的核心部分相當於關系代數,但又具有關系代數所沒有的許多特點,如聚集、資料庫更新等。它是一個綜合的、通用的、功能極強的關系資料庫語言。其特點是:
1、數據描述、操縱、控制等功能一體化。
2、兩種使用方式,統一的語法結構。SQL有兩種使用方式。一是聯機交互使用,這種方式下的SQL實際上是作為自含型語言使用的。另一種方式是嵌入到某種高級程序設計語言(如C語言等)中去使用。
前一種方式適合於非計算機專業人員使用,後一種方式適合於專業計算機人員使用。盡管使用方式不向,但所用語言的語法結構基本上是一致的。
3、高度非過程化。SQL是一種第四代語言(4GL),用戶只需要提出「干什麼」,無須具體指明「怎麼干」,像存取路徑選擇和具體處理操作等均由系統自動完成。
4、語言簡潔,易學易用。盡管SQL的功能很強,但語言十分簡潔,核心功能只用了9個動詞。SQL的語法接近英語口語,所以,用戶很容易學習和使用。
二、功能
SQL具有數據定義、數據操縱和數據控制。
1、SQL數據定義功能
能夠定義資料庫的三級模式結構,即外模式、全局模式和內模式結構。在SQL中,外模式有叫做視圖(View),全局模式簡稱模式( Schema),內模式由系統根據資料庫模式自動實現,一般無需用戶過問。
2、SQL數據操縱功能
包括對基本表和視圖的數據插入、刪除和修改,特別是具有很強的數據查詢功能。
3、SQL的數據控制功能
主要是對用戶的訪問許可權加以控制,以保證系統的安全性。
三、語句結構
結構化查詢語言包含6個部分:
1、數據查詢語言(DQL:Data Query Language)
其語句,也稱為「數據檢索語句」,用以從表中獲得數據,確定數據怎樣在應用程序給出。保留字SELECT是DQL(也是所有SQL)用得最多的動詞,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。這些DQL保留字常與其它類型的SQL語句一起使用。
2、數據操作語言(DML:Data Manipulation Language)
其語句包括動詞INSERT、UPDATE和DELETE。它們分別用於添加、修改和刪除。
3、事務控制語言(TCL)
它的語句能確保被DML語句影響地表的所有行及時得以更新。包括COMMIT(提交)命令、SAVEPOINT(保存點)命令、ROLLBACK(回滾)命令。
(1)資料庫三表聯合查詢句子擴展閱讀:
SQL的語言特點
1、SQL風格統一
SQL可以獨立完成資料庫生命周期中的全部活動,包括定義關系模式、錄入數據、建立資料庫、查詢、更新、維護、資料庫重構、資料庫安全性控制等一系列操作,這就為資料庫應用系統開發提供了良好的環境,在資料庫投入運行後,還可根據需要隨時逐步修改模式,且不影響資料庫的運行,從而使系統具有良好的可擴充性。
2、高度非過程化
非關系數據模型的數據操縱語言是面向過程的語言,用其完成用戶請求時,必須指定存取路徑。而用SQL進行數據操作,用戶只需提出「做什麼」,而不必指明「怎麼做」,因此用戶無須了解存取路徑,存取路徑的選擇以及SQL語句的操作過程由系統自動完成。這不但大大減輕了用戶負擔,而且有利於提高數據獨立性。
3、面向集合的操作方式
SQL採用集合操作方式,不僅查找結果可以是元組的集合,而且一次插入、刪除、更新操作的對象也可以是元組的集合。
參考資料來源:網路-結構化查詢語言
㈡ 「sqlserver」三表聯如何查詢「sql」語句
假設學生表叫student,課程表叫class,選課表叫choose
1.三層嵌套的問題
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一個內連接,一個嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一個聯合查詢,一個嵌套查詢
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.其實就是自連接查詢和行列交換的問題:
select student.id,
(case choose.id when '1' then choose.score end) as 1號課成績,
(case choose.id when '2' then choose.score end) as 2號課成績,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
㈢ 求三表聯合查詢的SQL查詢語句
1、SQL語句:select u.*,r.*,r.id rid
from user u left join sys_user_role sur on u.id = sur.useridleft join sys_role r on sur.roleid = r.id
圖片:(表名截圖)
算了,建表語句也給你們了,你們自己測試,這樣更詳細,(程序員)多動手,比什麼都好。(這里的 界面 對寫代碼不太友好,我放博客里了,自己復制粘貼測試使用就行)
sql語句地址:網頁鏈接
2、SQL語句解釋:
select a.*,b.*
from a表 a left join b表 b on a.id = b.aid
left join c表 c on b.cid = c.id
注2:此語句適合a表與c表連接,b表是關系表的情況。
㈣ 求sql語句多表(三表以上)聯合查詢
Select 單據主表.id,客戶名稱表.客戶名稱,單據編號,單據備注,本次欠款 from 單據主表 left join 客戶名稱表 on 單據主表.客戶id=客戶名稱表.id left join 單據名稱表 on 單據主表.單據類型=單據名稱表.單據名稱id
㈤ 三表聯合查詢語句,問了好多大神都不會,難道真不行嗎
tp中如何3張表關聯查詢,比如:a跟b有關聯關系,b跟c有關聯關系,而a跟c沒有直接的關聯關系,求大神指教!! 怎樣取得c表中的欄位值!!
評論(6)相關
x5342957392017年11月21日
$data['data']=$this->where($map)
->order("$orderby $orderway")
->field("a.*,b.brand_name,c.cat_name")
->alias('a')
->join('LEFT JOIN __BRAND__ b ON a.brand_id=b.id')
->join('LEFT JOIN __CATEGORY__ c ON a.cat_id=c.id')
->select()
復制代碼
x5342957392017年11月21日
兩個join就可以完美解決
guojie2016年06月15日
寫了報錯了,不存在該表
liangh2014年02月28日
三種方法:
1.採用關聯模型
2.像yybawang 說的.採用 join 方式查詢
3.寫普通的 SQL 語句 然後用 query() 來查詢
㈥ sqlserver三表聯查sql語句
假設學生表叫student,課程表叫class,選課表叫choose
1.三層嵌套的問題
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一個內連接,一個嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一個聯合查詢,一個嵌套查詢
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.其實就是自連接查詢和行列交換的問題:
select student.id,
(case choose.id when '1' then choose.score end) as 1號課成績,
(case choose.id when '2' then choose.score end) as 2號課成績,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
㈦ SQL三表聯合查詢的語句如何優化
selectt_cp.id,t_cp.proc,t_odid_cpid.num
fromt_odid_cpid
leftjoint_cpont_cp.id=t_odid_cpid.cpid
leftjoinT_ORDERont_odid_cpid.odid=T_ORDER.odid
WhereT_ORDER.B_zzdm='785390650'。
㈧ 三表聯查的SQL語句
這問題交給我吧,假設學生表叫student,課程表叫class,選課表叫choose
1.三層嵌套的問題
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一個內連接,一個嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一個聯合查詢,一個嵌套查詢
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.好吧,看起來很難,其實就是自連接查詢和行列交換的問題
select student.id,
(case choose.id when '1' then choose.score end) as 1號課成績,
(case choose.id when '2' then choose.score end) as 2號課成績,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
5.至於你說的insert報錯的問題,我想可能是因為學生ID和課程ID這兩個外鍵有重復的值,
你可以檢查下,實在不行刪除外鍵,插入數據,在這里外鍵對你最後要的結果影響不大。
純手工打造~有幫助記得給分哈
㈨ SQL三表關聯的查詢語句怎麼寫
Select * from a , b ,c where a.column1 = b.column1 and b.column2 = c.column2
當然.左連接也是可以的
關聯條件必須>=表數-1
㈩ SQL資料庫的表。怎麼同時連接3個表查詢。
可以參考下面的方法:
1、select * from 表1,表2,表3 where 表1.欄位=表2.欄位 and 表1.欄位=表3.欄位
2、select * from 表1 join 表2 on 表1.欄位=表2.欄位 and join 表3 on 表1.欄位=表3.欄位
如果沒有AND,前面就需要加括弧了。
(10)資料庫三表聯合查詢句子擴展閱讀:
參考語句
創建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據已有的表創建新表:
1、create table tab_new like tab_old (使用舊表創建新表)
2、create table tab_new as select col1,col2… from tab_old definition only
刪除新表
drop table tabname