irpas技术客

SQL语句select查询表的数据的简单查询和条件查询的常用方法_兴奋の大公猴_查询表

未知 5373

简单查询

1.查询表中的数据:

select * from 表的名字;

2.不查看表的数据,之查看表的结构

desc 表的名称;

3.查询一个字段

select 字段名 from 表名

4.查询多个字段,逗号隔开

select 字段名1,字段名2 from 表名;

5.给这段起别名

select 字段名1 as 别名1,字段名2 as 别名2 from 表名

6.字段可以使用数学运算

优化

条件查询

1.等于

select 字段 from 表名 where 字段=

2.不等于

select 字段 from 表名 where 字段 !=

或者

`select 字段 from 表名 where 字段 <>

3.小于

select 字段 from 表名 where 字段 <

4.小于等于

select 字段 from 表名 where 字段 <=

5.大于等于

select 字段 from 表名 where 字段 >

6.查询1-20的范围

select p_id,p_count from tb_product where p_price >=1 and p_price <= 20;

或者

mysql>select p_id,p_count from tb_product where p_price between 1 and 20;

7.is null(查询为null的字段),is not null(查询不为null的字段)

select p_id,p_count from tb_product where p_price is null;

8.and

select p_id,p_count from tb_product where p_price =4 and p_count>0;

9.or

select p_id,p_count,p_price from tb_product where p_price =4 or p_count=611;

(查找p_price=4或者p_count=611的字段)

10.找出p_count >0并且p_price=3或者p_price=4

select p_id,p_count,p_price from tb_product where p_count>0 and (p_price=3 or p_price=4);

注意:and优先级高,or要使用括号括起来

11.in(相当于多个or)

mysql> select p_id,p_count,p_price from tb_product where p_count in(876,611);

使用or的写法

mysql>select p_id,p_count,p_price from tb_product where p_count = 876 or p_count = 611;

12.not in

mysql> select p_id,p_count,p_price from tb_product where p_count not in(876,611);

13.like 称为模糊查询,支持%或下划线匹配 %匹配任意多个字符 下划线:任意一一个字符。 (8是一个特殊的符号,也是一 一个特殊符号)

13.1找出p_count以6结尾的

mysql> select p_count from tb_product where p_count like '%6';

13.2找出以4开头的p_count

mysql> select p_count from tb_product where p_count like '4%';

13.3找出第二个数为1的产品

mysql> select p_count from tb_product where p_count like '_1%';

13.4找出第三个数为6的产品

mysql> select p_count from tb_product where p_count like '__6%';

13.5找出p_count中含有6的产品

mysql> select p_count from tb_product where p_count like '%6%';


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #查询表