• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql多表联合查询语句是什么

mysql 搞代码 4年前 (2022-01-09) 26次浏览 已收录 0个评论

mysql多表联合查询语句是:使用select语句,union关键词,语法为【select 语句1 union [union 选项] select 语句2 union [union 选项] select 语句n】。

【相关学习推荐:mysql教程(视频)】

mysql多表联合查询语句是:

联合查询结果是将多个select语句的查询结果联合到一起。

可以使用union和union all关键字进行合并。

基本语法:

select 语句1

union [union 选项]

select 语句2

union [union 选项]

select 语句n

其中union选项有两个选项可选:all(表示重复也输出);distinct(去重,完全重复的,默认会去重)

两个表的字段一致即可。

例:
select id,addrid 
from addr 
union all 
select id,addrid 
from student

联合查询的意义

1.查询同一张表,但是需求不同

2.多表查询:多张表的结构完全一样,保存的数据(结构)也是一样的

联合查询order by的使用

在联合查询中:order by只能最后使用一个,需要对查询语句用括号才行。

例:
---(错误)
select * from student where sex="man" order by score
union
select * from student wherre sex="woman" order by score;
这种情况会报错,因为一个句子中不能有两个order by
---(正确但不符合所需)
select * from student where sex="man" 
union
select * from student wherre sex="woman" order by score;
这种情况是正确的,但是合并又没有意义,他会把之前的sex分好的情况给打乱
---(正确)
(select * from student where sex="man" order by sco<strong>本文来源gao@daima#com搞(%代@#码网</strong>re 
limit 10)
union
(select * from student wherre sex="woman" order by score
limit 10);
在子语句中使用order by,由于优先级的问题,需要将整个子句用()括起来,且必须和limit结合使用,否则不会生效。

想了解更多编程学习,敬请关注php培训栏目!

以上就是mysql多表联合查询语句是什么的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql多表联合查询语句是什么

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址