项目4-mysql数据表的检索

1. 基本查询

// 检索单个列

select prod_name from products;

// 检索所有列

select * from products;

// 检索不同的行

select distinct vend_id from products;

// 限制结果

select prod_name from products limit 4,5;

1.LIMIT :若有两个参数,则第一个参数为开始位置(从 0 行开始),第二个参数为返回结果的数量;若只有一个参数,则表示限制返回结果的数量。

2.除非确实需要表中的每个列,否则最好别使用 * 通配符,检索不需要的列会降低检索的性能。

2. 排序数据

// 对 `prod_name` 进行升序排序

select prod_name from products order by prod_name;

// 按多个列排序: 对 `prod_price` 降序排序,对 `prod_name` 升序排序

select prod_price, prod_name

form products

order by prod_price desc, prod_name;

如果没有明确规定排序顺序,MySQL 默认以数据添加到表中的顺序返回。

3.过滤数据

使用 WHERE 子句来指定搜索条件。

// 检查单个值,MySQL 在执行匹配时默认不区分大小写,因此 'jochen' 和 'Jochen' 都会匹配

select prod_name, prod_price from products where prod_name = 'jochen';

// 不匹配检查

select vend_id, prod_name from products where vend_id != 1000;

// 范围检查

select prod_name, prod_price

from products

where prod_price between 5 and 10;

// 空值检查

select cust_id from customers where cust_email is null;

// AND 操作符

select prod_id, prod_price

from produc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值