myql如何查詢有外來鍵的表,實現多表聯查?

2021-10-10 05:30:09 字數 1147 閱讀 9834

舉例子

create

table t_departments(

id int

primary

keyauto_increment

, name varchar(50

),location varchar

(128))

default

charset

= utf8;

create

table t_employees(

id int

primary

keyauto_increment

, name varchar(64

),salary double

, dept_id int

,foreign

key(dept_id)

references t_departments(id)

)default

charset

= utf8;

這裡有兩張表,第二張表設定了外來鍵

查詢這個department部門中有幾個員工

select t_departments.id,t_departments.name,t_departments.location,

t_employees.id emp_id,t_employees.name emp_name,t_employees.salary

from t_departments join t_employees

on t_departments.id = t_employees.dept_id

where t_departments.id =

1;

查詢結果

顯然是查到了這個部門下,所有的員工

資料資訊

如何查詢表外來鍵關聯表

查詢單個表的所有主外來鍵關係 select a.owner 主鍵擁有者 a.table name 主鍵表 b.column name 主鍵列 c.owner 外來鍵擁有者 c.table name 外來鍵表 d.column name 外來鍵列 from user constraints a lef...

如何繫結外來鍵實現多表查詢

例如有兩個表分別為 book 書籍 與 publisher 出版社 create table publisher 建立table publisher id char 36 primary key,name varchar 30 not null,address varchar 120 create ...

查詢表主鍵 外來鍵

專案中用到的一些sql oracle下的 總結 1 查詢表的所有索引 包括索引名,型別,構成列 select t.i.index type from user ind columns t,user indexes i where t.index name i.index name and t.tab...