本文目录一览:
- 1、mysql数据库,多个表的查询操作
- 2、MySQL 多表查询
- 3、mysql多表查询sql语句怎么写
- 4、MySQL多表查询(超过10个相关的表)
- 5、mysql数据库多表查询问题
- 6、mysql数据库多表查询的问题
mysql数据库,多个表的查询操作
要点:left
join,right
join,inner
join
首先有如下两个表:
student:
id(int)
name(nvarchar)
1
a
2
b
3
c
4
d
5
e
6
f
quiz:
id(int)
score(int)
1
60
2
70
4
80
6
90
8
100
9
30
内连接:(inner
join)包括连接表的匹配行
select
student.name,quiz.score
from
quiz
inner
join
student
on
student.id=quiz.id
name
score
a
60
b
70
d
80
f
90
左连接:(left
join)包括连接表匹配行以及左连接表的所有行
select
student.name,quiz.score
from
student
left
join
quiz
on
student.id=quiz.id
name
score
a
60
b
70
c
null
d
80
e
null
f
90
右连接:(right
join)结果包括连接表的匹配行以及右连接表的所有行
select
student.name,quiz.score
from
student
right
join
quiz
on
student.id=quiz.id
name
score
a
60
b
70
d
80
f
90
null
100
null
30
当然,也可以看出左连接也可以写成右连接的形式:
select
student.name,quiz.score
from
student
right
join
quiz
on
student.id=quiz.id等价于
select
student.name,quiz.score
from
quiz
left
join
student
on
student.id=quiz.id
MySQL 多表查询
我不是很了解你想要的是怎么样。以下是我的思路:select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id 查出单选
select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id 查出多选
select S.name, S.description, S.status, C1.topic, C1.qid, C1.options, C1.description from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_id 单选合并到问卷
select S2.name, S2.description, S2.status, C2.topic, C2.qid, C2.options, C2.description from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id) as C2 on S2.id = C2.survey_id 多选合并到问卷
select S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_id 简答表合并到问卷
如果你是要多行列出 问卷名 题号 题目select S.name, S.description, S.status, C1.topic, C1.qid from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_idUNION ALLselect S2.name, S2.description, S2.status, C2.topic, C2.qid from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.multiple_choices) as C2 on S2.id = C2.survey_idUNION ALLselect S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_idORDER BY name, qid ASC
如果是一条列出select * from (select * from (select S.name, S.description, S.status, C1.topic, C1.qid, C1.options, C1.description from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_id) as D left join (select S2.name, S2.description, S2.status, C2.topic, C2.qid, C2.options, C2.description from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id) as C2 on S2.id = C2.survey_id) as D1 on D.name = D1.name) as E left join (select S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_id) E1 on E.name = E1.name
(注意:要修改*号列出你想列的列名,并改一下选项里面的列名)
mysql多表查询sql语句怎么写
一使用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
使用表的别名应注意几下几点
(1)别名通常是一个缩短了的表名,用于在连接中引用表中的特定列,如果连接中的多个表中有相同的名称列存在,必须用表名或表的别名限定列名
(2)如果定义了表的别名就不能再使用表名
三合并多个结果集
SQL语言中,可以通过UNION 或 ALL将多个SELECT语句的查询结果合并输出,这两个关键字的使用说明如下:
UNION:利用该关键字可以将多个SELECT 语句的查询结果合并输出,并删除重复行
ALL:利用该关键字可以将多个SELECT 语句的查询结果合并输出,但不会删除重复行
在使用UNION或ALL关键字将多个表合并输出时,查询结果必须具有相同的结构并且数据类型必须兼容,另外使用UNION时两张表的字段数量也必须相同,否则会提示SQL语句有错误。
e.x:SELECT id,name,pwd FROM tb_demo067 UNION SELECT uid,price,date FROM tb_demo067_tel
四简单嵌套查询
子查询:子查询是一个SELECT查询,返回单个值且嵌套在SELECT、INSERT、UPDATE和DELETE语句或其它查询语句中,任何可以使用表达式的地方都可以使用子查询.
SELECT id,name,sex,date FROM tb_demo068 WHERE id in(SELECT id FROM tb_demo068 WHERE id='$_POST[test]')
内连接:把查询结果作为WHERE子句的查询条件即称为内连接
五复杂的嵌套查询
多表之间的嵌套查询可以通过谓词IN实现,语法格式如下:
test_expression[NOT] IN{
subquery
}
参数说明:test_expression指SQL表达式,subquery包含某结果集的子查询
多表嵌套查询的原理:无论是多少张表进行嵌套,表与表之间一定存在某种关联,通过WHERE子句建立此种关联实现查询
六嵌套查询在查询统计中的应用
实现多表查询时,可以同时使用谓词ANY、SOME、ALL,这些谓词被称为定量比较谓词,可以和比较运算符联合使用,判断是否全部返回值都满足搜索条件.SOME和ANY谓词是存在量的,只注重是否有返回值满足搜索条件,这两个谓词的含义相同,可以替换使用;ALL谓词称为通用谓词,它只关心是否有谓词满足搜索要求.
SELECT * FROM tb_demo069_people WHERE uid IN(SELECT deptID FROM tb_demo069_dept WHERE deptName='$_POST[select]')
SELECT a.id,a.name FROM tb_demo067 AS a WHERE id3)
ANY 大于子查询中的某个值
=ANY 大于等于子查询中的某个值
=ANY 小于等于子查询中的某个值
=ANY 等于子查询中的某个值
!=ANY或ANY 不等于子查询中的某个值
ALL 大于子查询中的所有值
=ALL 大于等于子查询中的所有值
=ALL 小于等于子查询中的所有值
=ALL 等于子查询中的所有值
!=ALL或ALL 不等于子查询中的所有值
七.使用子查询作派生的表
在实际项目开发过程中经常用到从一个信息较为完善的表中派生出一个只含有几个关键字段的信息表,通过子查询就可以来实现这一目标,如
SELECT people.name,people.chinese,people.math,people.english FROM (SELECT name,chinese,math,english FROM tb_demo071) AS people
注:子查询应遵循以下规则:
(1)由比较运算符引入的内层子查询只包含一个表达式或列名,在外层语句中的WHERE子句内命名的列必须与内层子查询命名的列兼容
(2)由不可更改的比较运算符引入的子查询(比较运算符后面不跟关键字ANY或ALL)不包括GROUP BY 或 HAVING子句,除非预先确定了成组或单个的值
(3)用EXISTS引入的SELECT列表一般都由*组成,不必指定列名
(4)子查询不能在内部处理其结果
八使用子查询作表达式
SELECT (SELECT AVG(chinese)FROM tb_demo071),(SELECT AVG(english)FROM tb_demo071),(SELECT AVG(math)FROM tb_demo071) FROM tb_demo071
注:在使用子查询时最好为列表项取个别名,这样可以方便用户在使用mysql_fetch_array()函数时为表项赋值,如
SELECT (SELECT AVG(chinese) FROM tb_demo071) AS yuwen ,(SELECT AVG(english) FROM tb_demo071) AS yingyu,(SELECT AVG(math) FROM tb_demo071) AS shuxue FROM tb_demo071
九使用子查询关联数据
SELECT * FROM tb_demo072_student WHERE id=(SELECT id FROM tb_demo072_class WHERE className = '$_POST[text]')
十多表联合查询
利用SQL语句中的UNION,可以将不同表中符合条件的数据信息显示在同一列中。
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten
注:使用UNION时应注意以下两点:
(1)在使用UNION运算符组合的语句中,所有选择列表的表达式数目必须相同,如列名、算术表达式及聚合函数等
(2)在每个查询表中,对应列的数据结构必须一样。
十一对联合后的结果进行排序
为了UNION的运算兼容,要求所有SELECT语句都不能有ORDER BY语句,但有一种情况例外,那就是在最后一个SELECT语句中放置ORDER BY 子句实现结果的最终排序输出。
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten ORDER BY id
使用UNION条件上相对比较苛刻,所以使用此语句时一定要注意两个表项数目和字段类型是否相同
十二条件联合语句
SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name='人民邮电出版社' OR name='机械工业出版社' UNION SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name '人民邮电出版社' AND name '机械工业再版社' ORDER BY id
上面语句应用了GROUP BY分组语句和HAVING语句实现条件联合查询。其实现目的是先保证将'人民邮电出版社'和'机械工业出版社'始终位于名单最前列,然后再输出其它的出版社
十三简单内连接查询
SELECT filedlist FROM table1 [INNER] JOIN table2 ON table1.column1 = table2.column1
其中,filedlist是要显示的字段,INNER表示表之间的连接方式为内连接,table1.column1=table2.column1用于指明两表间的连接条件,如:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id
十四复杂内连接查询
复杂的内连接查询是在基本的内连接查询的基础上再附加一些查询条件,如:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id WHERE b.id=(SELECT id FROM tb_demo065 WHERE tb_demo065.name='$_POST[text]')
总之,实现表与表之间的关联的本质是两表之间存在共同的数据项或者相同的数据项,通过WHERE 子句或内连接INNER JOIN … ON 语句将两表连接起来,实现查询
十五使用外连接实现多表联合查询
(1)LEFT OUTER JOIN表示表之间通过左连接方式相互连接,也可简写成LEFT JOIN,它是以左侧的表为基准故称左连接,左侧表中所有信息将被全部输出,而右侧表信息则只会输出符合条件的信息,对不符合条件的信息则返回NULL
e.x:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A LEFT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
(2)RIGHT OUTER JOIN表示表之间通过右连接方式相互连接,也可简写成RIGHT JOIN,它是以右侧的表为基准故称右连接,右侧表中所有信息将被全部输出,而左侧表信息则只会输出符合条件的信息,对不符合条件的信息则返回NULL
E.X:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A RIGHT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
十六利用IN或NOTIN关键字限定范围
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code BETWEEN '$_POST[text1]' AND '$_POST[text2]')
利用IN可指定在范围内查询,若要求在某范围外查询可以用NOT IN代替它
十七由IN引入的关联子查询
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code = '$_POST[text]')
十八利用HAVING语句过滤分组数据
HAVING子句用于指定组或聚合的搜索条件,HAVING通常与GROUP BY 语句一起使用,如果SQL语句中不含GROUP BY子句,则HAVING的行为与WHERE子句一样.
e.x:SELECT name,math FROM tb_demo083 GROUP BY id HAVING math '95'
MySQL多表查询(超过10个相关的表)
你可以试试下面这个:
select * from table as a inner (join) table_2 as b on (a.ch=b.ch) inner (join) table_3 as c on (c.ch=b.ch) inner (join) table_4 as d on (d.ch =c.ch) inner (join) table_5 as e on (e.ch = d.ch) inner (join) table_6 as f on (f.ch =e.ch) inner (join) table_7 as g on (g.ch=f.ch) inner (join) table_8 as h on (h.ch=g.ch) inner (join) table_9 as i on (i.ch =h.ch);
其实这个sql的查询命令你可以用一个循环来做,将字符串搞好就可以了。
mysql数据库多表查询问题
员工表emp,部门表dept
select empName,
(select deptName from dept where deptid=dep1) as deptName1,
(select deptName from dept where deptid=dep2) as deptName2,
(select deptName from dept where deptid=dep3) as deptName3,
(select deptName from dept where deptid=dep4) as deptName4,
(select deptName from dept where deptid=dep5) as deptName5
from emp;
mysql数据库多表查询的问题
你可以这样查
select
*
from
goods,category
where
goods.c_id==category.c_id
我不知道你的具体情况,给你个列子,这样就可以查询多表了,希望对你有帮助