mysql查询语句的总结
技术百科
PHP中文网
发布时间:2017-06-20
浏览: 次 1、基本构成
(1)需要查询的表(单表,多表)
(2)需要查询的信息(字段信息,过滤处理)
(3)查询条件(字段关联,字段值范围,记录截取设置,排序方式,分组方式,去重,or ,and)
2、实例展示(以user表为例)
2.1查询单表(user)
(1)查询单表所有字段
select * from user;(select后面跟字段名,from后面跟表名,*代表所有字段, where后面跟条件)
(2)查询单表特定字段
select user_id,user_name from user;(字段名之间用“,”隔开)
(3)查询单表记录总数count(),sum(),max(),min()用法相同,后面三个函数参数不能为*。
select count(*) from user;
(4)查询单表,按user_id分组统计每组记录总数,并按照user_id倒序
select count(*) from user group by user_id desc;
注意:分组字段只有一个时,可直接在后面加desc进行倒序,默认是正序,也可加上asc
(5)查询单表,按user_id,user_name分组统计每组记录总数,并按照user_id倒序
select count(*) from user group by user_id,user_name order by user_id desc;
注意:group by与order by同时使用,排序的字段user_id要出现在分组字段(user_id,user_name)中
(6)查询单表,条件为某个字段值范围
user_id>=1并且=1 and user_
id
user_id在1和2之间 :select * from user where user_id between 1 and 2;
user_id包含于(1,2):select * from user where user_id in(1,2);
user_id是1或2 :select * from user where user_id=1 or user_id=2;
(7)查询单表,截取数据limit index,length
截取第1条:select * from user limit 1;或者select * from user limit 0,1;
截取第2条:select * from user limit 1,1;
(8)查询单表,去重distinct
select distinct user_name from user;
(9)having关键字,可以与合计函数一起使用;
select count(*) from user group by user_id desc having max(user_weight)
2.2查询多表(user,order)
(1)inner join(只返回匹配值)
select * from user inner join order on user.user_id=order.user_id;
(2)left join(返回匹配值和左表剩余值)
select * from user u left join order o on u.user_id=o.user_id;
注意:u和o是别名,方面使用
(3)right join(返回匹配值和右表剩余值)
select * from user right join order on user.user_id=order.user_id;
(4)full join(返回所有值)
select * from user full join order on user.user_id=order.user_id;
# 出现在
# 能为
# 只有一个
# 为例
# 也可
# 可直接
# mysql
# 字段名
# 每组
# 在后面
# group
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何在Golang中实现WebSocket广播_使
- Win11怎么检查TPM2.0模块_Windows
- C++如何使用std::async进行异步编程?(
- Win10 BitLocker加密教程 Win10
- Win11怎么开启智能存储_Windows11存储
- Django 密码修改后会话失效的解决方案
- win11 OneDrive怎么彻底关闭 Win1
- 如何提升Golang JSON序列化性能_Gola
- c++中如何使用auto关键字_c++11类型推导
- 如何使用Golang log设置日志输出格式_Go
- 如何使用Golang搭建Web开发环境_快速启动H
- Mac的“调度中心”与“空间”怎么用_Mac多桌面
- 短链接怎么用php递归还原_多层加密链接的处理法【
- Python装饰器复用技巧_通用能力解析【教程】
- PHP主流架构如何处理会话管理_Session与C
- c++中的可变参数模板(variadic temp
- 如何在Golang中引入测试模块_Golang测试
- c++如何判断文件是否存在_c++ filesys
- Win11怎么开启上帝模式_创建Windows 1
- Win11更新后变慢怎么办_Win11系统更新后卡
- Win11怎么制作U盘启动盘_Win11原版系统安
- Python脚本参数接收_sys与argparse
- 如何在Golang中捕获HTTP服务器错误_Gol
- 如何更改Windows资源管理器的默认启动位置?(
- 如何使用Golang实现容器安全扫描_Golang
- Windows系统文件被保护机制阻止怎么办_权限不
- Windows资源管理器总是卡顿或重启怎么办?(修
- Python函数接口文档化_自动化说明【指导】
- php怎么下载安装后测试是否成功_简单脚本验证方法
- PHP主流架构怎么集成Redis缓存_配置步骤【方
- Python函数缓存机制_lru_cache解析【
- Win11怎么关闭透明效果_Windows11个性
- php485能和物联网模块通信吗_php485对接
- Windows Defender扫描失败怎么办_安
- php删除数据怎么加限制_带where条件删除避免
- Win11系统占用空间大怎么办 Win11深度瘦身
- php订单日志怎么记录物流_php记录订单物流变更
- Python路径拼接规范_跨平台处理说明【指导】
- Win11怎么设置任务栏图标大小_Windows1
- Win11怎么设置虚拟桌面 Win11新建多桌面切
- PHP接收参数值为空怎么办_判断和处理空参数方法说
- 如何正确访问 Laravel 模型或对象的属性而非
- Win11怎么关闭任务栏小图标_Windows11
- Avalonia如何实现跨窗口通信 Avaloni
- 如何在JavaScript中动态拼接PHP的bas
- Win11文件扩展名怎么显示_Win11查看文件后
- Win11怎么查看显卡显存_查询Win11显卡详细
- php和redis连接超时怎么办_phpredis
- Django密码修改后会话失效的解决方案
- Python安全爬虫设计_IP代理池与验证码识别策

QQ客服