数据库操作
添加数据
intsert into tablename (id,name,gae) values(value1,value2,value3); //插入数据,这里还不能中文;
改变数据
update tablename set age=vlaue1,name=value2 where id=value3; //改变数据内容
删除数据
delete from tablename where id=value1;//删除id为value的数据;
select * from studytest; //查询所有数据;
select id,name from studytest;//查询对应数据
select distinct name from studytest;//查询name,去除重复;
select ifnull(name,'无名') from tablename;//name 如果为空,显示无名;
select concat("姓名是",name) from tablename;//查询name加字符串
select name 姓名 from tablename;//给查询结果加名字;
模糊查询
select * from name from tablename where name like '_';//名字只有一个的字的,_代表一个字符
select * from name from tablename where name like '%李%';//名字中含有李字的,%代表0到n个字符
排序查询
select * from tablename order by age ASC ; //通过age来升序结果;
select *from tablename order by age desc;//通过age来降序结果;
select *from tablename order by age desc,id ASC;//通过age来降序结果,如果age相等通过id来降序;
聚合函数
select count(*) from tablename; //一共有多少数据;
select sum(age) from tablename;//一共多少岁;求和 ,同理 max(age) 求最大。min(age) 求最小;avg(age) 求平均。
分组
select age ,count(*) from tablename group by age;//通过age分组;
分页
select * from tablename limit 0,10; //只要前10条数据;
简单顺序总结
select * from tablename
where 条件 //查询条件
group by ....//分组条件
having .... //分组后的条件
order by ....; //排序条件
解决无法输入中文问题
show variables like 'cahr%'; //查看编码数据
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | utf8 | //客户端输入编码
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 | //结果输出编码
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:\work\mySql\share\charsets\ |
修改输入编码;
set charecter_set_client=gbk; //可以输入中文,只在当前窗口有效;输出是乱码,应为窗口输出是utf8;
正确方法:
修改mysql安装目录下的my.ini文件
[client]
port=3306
[mysql]
default-character-set=uft8
改为default-character-set=gbk
重启mysql
exit;\\ 退出mysql;
net stop mysql;//停止mysql服务
net start mysql;//开始mysql服务
重新进入,配置生效;