mysql多表查询的三种方法?
mysql多表查询的方法可大致分为join连接、直接关联和子查询三种方式,下面简单介绍下。
1、join连接,语法为:select ... from tables join tableb。分为内连接、外连接、和左右连接四种。
2、直接关联,语法为:select ... from tables tableb。实现效果等同于内连接。
3、子查询,语法为:select ... from tables where (select ... from tableb) as b。实现效果也等同于内连接。
怎样把两个不同数据库中的表做关联查询呢?
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;
“mysql”多表联合查询语句怎么写?
一使用SELECT子句进行多表查询
SELECT 字段名 FROM 表1,表2 … WHERE 表1.字段 = 表2.字段 AND 其它查询条件
SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id
注:在上面的的代码中,以两张表的id字段信息相同作为条件建立两表关联,但在实际开发中不应该这样使用,最好用主外键约束来实现
二使用表的别名进行多表查询
如:SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id AND b.id='$_POST[textid]'
SQL语言中,可以通过两种方式为表指定别名
第一种是通过关键字AS指定,如
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 AS a,tb_demo065_tel AS b WHERE a.id=b.id
第二种是在表名后直接加表的别名实现
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id

