mysql之单表查询__我自己敲的代码

1、select * from person; #查看全部

2、select name,sex from person;   #只选择name和sex这两列name和sex之间有一个逗号的

3、select name,sex as '性别' from person #将name和sex两列选择出来,并将sex这一列的列名字改名字为性别,或者是为字段起别名

4、select salary+200 from person;  #将person中的salary列选出来,并将salary中数据都加200

5、select DISTINCT age from person;  #将age这一列中重复的数据去除,或者是去重查询

条件查询: where为关键字,后面跟条件

(1)运算符:

6、select * from person where age>20    #查找出age列中年龄大于20的

另外一些逻辑符号:>=表示大于等于   <>表示不等于   !=也是表示不等于

(2)关键字:null

7、select * from person where dept_id is null; #查找dept_id列中没有值的那个人

     另:select * from person where dept_id is null; #查找dept_id列中有值的人

null 用来判断一个数字是不是为空 如上面

‘ ’  用来表示空的字符串

如  select * from person where name=''; #查找name列中没有名字的那一个人

 

person表中我删除了 一个人的名字,那就必须用 =‘ ‘来查询了 因为name为字符串

(3)区间查询

 8、select * from person where age=28 and salary>5000; #查找年龄为28岁且工资大于5k的人

另外的逻辑运算符: or 或 ; and 与; 

 select * from person where not(age=28 and salary>5000); #查找年龄 不 为28岁且工资大于5k的人    非

9、select * from person where age BETWEEN 18 and 20; #查找age列中年龄在18和20之间的人 BETWEEN 18 and 20表示18和20之间

10、select * from person where id in(1,3,5);  #查找id=1,3,5的人

       select * from person where id=1 or id=3 or id=5;  #也可以用这一个代替

11、select * from person where name like 'e%'; #查找名字以字母e开头的人

 12、select * from person where name like '%e%'; #查找名字中包含字母e的人

 

13、select * from person where name like '%e'; #查找名字以e结尾的人

14、select * from person where name like '__e%'; #e前边有两个下划线,一个下划线表示一个占位符(任意的字母) 所以两个下划线就表示两个任意的字母,这句话的意思就是查找第三个字母为e的人

15、select * from person where name like '____%';  #有四个下划线,表示名字为四个字符的人

(4)排序查询

16、select * from person ORDER BY salary;  #将salary中的数据进行排序 

select * from person ORDER BY salary esc;  #将salary中的数据进行排序 默认为esc,esc可以不用写,esc表示正序排序

select * from person ORDER BY salary desc;  #desc表示将salary中的数据进行倒序排序 

多条件的要把order by写在后边,如

select * from person where age>20  ORDER BY salary desc;  #将age列中数据大于20的数据选出来之后对salary进行排序

17、中文(uft8)不支持排序, 但是gbk支持中文排序

CONVERT(name USING gbk)  #将name中的数据编码格式转为gbk格式的

 select * from person ORDER BY convert(name USING gbk);  #将namme转为gbk之后进行排序,其中字母和汉字是分开排序的

 

18、show VARIABLES like 'char%'; #查询各种编码方式

18、mysql中安装目录中没有my.ini的解决方法:

自己创建一个my.ini  然后把下面的复制进去,再重启服务(net start mysql) 即可

#修改方法:
#1. 创建my.ini文件,放在mysql根路径下
#2. 在该文件中添加以下内容即可:
#3.添加此文件后需要重新启动服务,以保证此文件生效
------------------------------------------------------------
[client]

default-character-set=utf8

[mysql]

#设置mysql客户端默认字符集

default-character-set=utf8

[mysqld]

#设置3306端口

port = 3306

#允许最大连接数

max_connections=200

#服务端使用的字符集默认为8比特编码的latin1字符集

character-set-server=utf8

#创建新表时将使用的默认存储引擎

default-storage-engine=INNODB

#解决mysql在执行sql语句后出现1055错误,sql_mode = only_full_group_by不相容

sql_mode='NO_ENGINE_SUBSTITUTION'
my.ini中的内容

2019.01.05  昨天更改结束了数字信号的考试,考的贼差,考完有种会挂科的感觉,这几天一直在想如果挂科会怎么样,真的是很难过......

今天终于搞定了考试之前没有搞定的Navicat for mysql这个软件,并且学了一些sql语言相关的东西,收获还是很多的

当然晚饭回来看了一下别人给我推荐的电影  重庆森林  王菲真的是好可爱~

haijing in HZ 

good night      

猜你喜欢

转载自www.cnblogs.com/YiYA-blog/p/10226668.html