sql三表连接查询 - 使用sqlite 演示
表;
人員表,職位表,部門表;
現在要做的是:列出所有人員,以及他們所屬的部門和職位;
由于要列出tb_user中的每一條記錄,需要用左連接查詢。這里要連接3個表;
代碼如下;
SELECT u.sName,p.sCaption,d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos AS p LEFT JOIN ?tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;
運行;
提示 p.sCaption ,沒有此列;
檢查一下sql;應該沒錯;
可能括號里面又有別名,這種sql寫法,sqlite不能識別;
換個寫法;
select tb_user.sName,tb_pos.sCaption, tb_dpt.sCaption FROM tb_user left join tb_pos on tb_user.id_Pos=tb_pos.id left join tb_dpt on tb_pos.id_Dpt=tb_dpt.id;
運行,結果有了;
?
外連接(OUTER JOIN)是內連接(INNER JOIN)的擴展。雖然 SQL 標準定義了三種類型的外連接:LEFT、RIGHT、FULL,但 SQLite 只支持 左外連接(LEFT OUTER JOIN)。
參閱;
? ? ? ? https://www.runoob.com/sqlite/sqlite-joins.html
全部的cmd操作如下;
Microsoft Windows [版本 6.1.7601]
Copyright (c) 2010 Microsoft Corporation. ?All rights reserved.
S:\6\sqlite>sqlite3 test1.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .database
seq ?name ? ? ? ? ? ? file
--- ?--------------- ?----------------------------------------------------------
0 ? ?main ? ? ? ? ? ? S:\6\sqlite\test1.db
sqlite> create table tb_user (
? ?...> id int primary key not null,
? ?...> sName varchar(50),
? ?...> id_Pos);
sqlite> create table tb_pos (
? ?...> id int primary key not null,
? ?...> sCaption varchar(50),
? ?...> id_Dpt int);
sqlite> create table tb_dpt (
? ?...> id int primary key not null,
? ?...> sCaption varchar(50));
sqlite> .tables
tb_dpt ? tb_pos ? tb_user
sqlite> insert into tb_user (1,'李一一',2);
Error: near "1": syntax error
sqlite> insert into tb_user (1,"李一一",2);
Error: near "1": syntax error
sqlite> .shcema tb_user
Error: unknown command or invalid arguments: ?"shcema". Enter ".help" for help
sqlite> .schema tb_user
CREATE TABLE tb_user (
id int primary key not null,
sName varchar(50),
id_Pos);
sqlite> drop table tb_user;
sqlite> create table tb_user (
? ?...> id int primary key not null,
? ?...> sName varchar(50),
? ?...> id_Pos int);
sqlite> insert into tb_user values (1,'李一一',2);
sqlite> insert into tb_user values (2,'王不二',3);
sqlite> insert into tb_user values (3,'孫六七',1);
sqlite> insert into tb_pos values (1, '經理', 0);
sqlite> insert into tb_pos values (2, '程序', 1);
sqlite> insert into tb_pos values (3, '', 1);
sqlite> insert into tb_dpt values (1, '設計部');
sqlite> insert into tb_dpt values (2, '運維部');
sqlite> select * from tb_user;
1|李一一|2
2|王不二|3
3|孫六七|1
sqlite> select * from tb_pos;
1|經理|0
2|程序|1
3||1
sqlite> select * from tb_dpt;
1|設計部
2|運維部
sqlite> SELECT u.sName p.sCaption d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos
?AS p LEFT JOIN tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;
Error: near ".": syntax error
sqlite> SELECT u.sName p.sCaption d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos
?AS p LEFT JOIN ?tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;
Error: near ".": syntax error
sqlite> SELECT u.sName p.sCaption d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos
?AS p LEFT JOIN ?tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;
Error: near ".": syntax error
sqlite> SELECT u.sName,p.sCaption,d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos
?AS p LEFT JOIN ?tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;
Error: no such column: p.sCaption
sqlite> .schema tb_pos
CREATE TABLE tb_pos (
id int primary key not null,
sCaption varchar(50),
id_Dpt int);
sqlite> select tb_user.sName,tb_pos.sCaption, id_Dpt.sCaption FROM tb_user left
join tb_pos on tb_user.id_Pos=tb_pos.id left join tb_dpt on tb_pos.id_Dpt=tb_dpt
.id;
Error: no such column: id_Dpt.sCaption
sqlite> select tb_user.sName,tb_pos.sCaption, tb_dpt.sCaption FROM tb_user left
join tb_pos on tb_user.id_Pos=tb_pos.id left join tb_dpt on tb_pos.id_Dpt=tb_dpt
.id;
李一一|程序|設計部
王不二||設計部
孫六七|經理|
sqlite>
?
總結
以上是生活随笔為你收集整理的sql三表连接查询 - 使用sqlite 演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解在emu8086中学习汇编语言数字比
- 下一篇: 词法分析器和lex工具基本学习