码迷,mamicode.com
首页 > 数据库 > 详细

mysql(2)sql语句的入门

时间:2018-08-11 01:12:22      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:查找   sql语句   creat   一个   ifnull   order by   内容   data   表名   

1.数据的导入

创建数据库;

Select version();//查看当前数据库版本(假如数据库升级,加了-s,没有对原数据进行重构,只对系统表进行了重构,那么数据库的版本可能不是当前的版本);

Create  database 数据库名称;//创建数据库

Show databases ; //显示所有的数据库,产看表是否已经导入

Use   刚刚创建的数据库;

Source  需要导入的表(文件名);//导入表

Show tables ;//显示该库下的表

Desc   原表名(不是文件名);//显示表的结构

Select  *  from  表名;  //查看表内容

 

2.数据库及表的创建以及删除

数据库的创建

Create  database 数据库名称;//创建数据库

数据库的删除

drop  database  数据库名;

Create table 表名(

字段1  修饰(范围,这个是所有的都有范围)

 

删除表

Drop  table  表名;

 

3.数据库及表的查询

数据库

Show databases  ;  查询所有的表

show databases like ‘%es%’ ;//模糊查找过滤;

select  database();  查看当前处于哪个数据库;

Use  test ;//使用库

Show tables ;//显示该库下的所有表

show  tables  like  ‘%es%’;//在数据库test下模糊查找具有es字段的表;

show  create  table 表名 ;//查看怎么创建的表

 

4.mysql的退出

Quit, exit , ctrl +c

5.select  语句

Select  字段1,字段2...  from  表名   //查询表中所有的字段1,字段2...信息

Select  字段1,字段2...  from  表名  where  条件 ;//查询表中符合条件的字段1,字段2...信息

例如select  name  ,age  from employee where  name> 35 ;// 查询表中age大于35的name age的信息;

对select可进行如下一些操作

算术运算符

+,-,*,/,%

例如:

select  salary*12  as  ‘年薪’  from  employees ;  //as 输出显示的别名,可以省略不写,其它运算符类似。

select  salary*12  as  ‘年薪’  from  employees  where  salary>10000*3;

关系运算符

==,>,</>=,<=,!=,between a and  b

例如: select  salary    from  employees  where  salary>10000; // 其它类似;

select  salary    from  employees  where  salary  between 1000 and10000;

排序

oder  by  【数字/字段名】【asc/desc】,数字/字段名】【asc/desc】

例如:select  name   from  employees  order by  1  asc;//1表示第一个字段

 select  name  salary  from  employees  order by  1  asc;2 desc//按照那么升序,salary降序;

is null , in ,not ,like

select  name   from  employees  where  name  in(‘张三’,‘李四’);//是张三或者李四

select  name   from  employees  where  name  is null;//null 表示什么都没有

select  name   from  employees  where  name  like ‘%s_s%’;//模糊查找,%代表一个或者多个字符,_代表一个字符

select  name   from  employees  where  name  not  in(‘张三’,‘李四’);//不是张三或者李四

select  name   from  employees  where  name  is  not  null;//表示不是null

数据处理函数

trim,upper,lower,substr(被处理字段,下标,长度),length,round,rand,ifnull(字段,如果为空的默认值);

mysql(2)sql语句的入门

标签:查找   sql语句   creat   一个   ifnull   order by   内容   data   表名   

原文地址:https://www.cnblogs.com/gg128/p/9457971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!