sql语句多表关联怎么查询?

sql语句多表关联怎么查询?,第1张

用SELECT对多表关联进行查询。

SQL是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。

结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同 数据库系统,,可以使用相同的结构化查询语言作为数据输入与管理的接口。结构化查询语言语句可以嵌套,这使它具有极大的灵活性和强大的功能。

mysql支持多个库中不同表的关联查询,你可以随便链接一个数据库

然后,sql语句为:

select * from db1.table1 left join db2.table2 on db1.table1.id = db2.table2.id

只要用数据库名加上"."就能调用相应数据库的数据表了.

数据库名.表名

扩展资料

mysql查询语句

1、查询一张表:     select * from 表名;

2、查询指定字段:select 字段1,字段2,字段3....from 表名;

3、where条件查询:select 字段1,字段2,字段3 frome 表名 where 条件表达式;

例:select * from t_studect where id=1

  select * from t_student where age>22

4、带in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (21,23)     

   select * from t_student where age not in (21,23)

5、带between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29

     select * frome t_student where age not between 21 and 29

比如有三张表\x0d\x0adept部门表(dept_id,dept_name)、\x0d\x0aemp_info(emp_id,emp_name,dept_id,role_id)用户信息表、\x0d\x0aemp_role(role_id,role_name)管理表\x0d\x0a \x0d\x0adept部门表的dept_id等于emp_info用户信息表dept_id,\x0d\x0a而emp_role管理表的role_id等于emp_info用户信息表role_id\x0d\x0a \x0d\x0a如果想查用工的部门名dept_name、姓名emp_name和职务名role_name并按员工ID排序\x0d\x0a那只能连接三个表,语句如下\x0d\x0a \x0d\x0aselect dept.dept_name,emp_info.emp_name,emp_role.role_name \x0d\x0afrom dept join emp_info e \x0d\x0aon dept.dept_id=emp_info.dept_id \x0d\x0ajoin emp_role\x0d\x0aon emp_info.role=emp_role.role_id\x0d\x0aorder by emp_info.emp_id


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6473696.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-23
下一篇2023-03-23

发表评论

登录后才能评论

评论列表(0条)

    保存