0.mysql中常用命令
**#1查看当前所在的数据库:
SHOW DATABASES;
#2使用指定的库:
USE 库名;
#3打开当前库所有的表:
SHOW TABLES FROM 库名;
#4查看表结构:
DESC 表名;
#5查看服务器版本:
SELECT VERSION();
#6注释
#单行注释
/*
...
多行注释
...
*/**
基础查询
1.去重:DISTINCT(distinct)
语法:select DISTINCT 查询字段 FROM 表名;
#案例
mysql> select distinct department_id from employees;
+---------------+
| department_id |
+---------------+
| NULL |
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
+---------------+
12 rows in set (0.06 sec)
2. +号的作用
有且仅有一个功能:运算符
只要其中一方为null,则结果一定为null(select null+100;)结果为null
**两个操作数都为数值型,做加法运算**
select 100+90;
+--------+
| 100+90 |
+--------+
| 190 |
+--------+
**其中一方为字符型,则试图转换字符型为数值型,成功则做加法运算;
失败则将字符型数值转换为0**
mysql> select '87'+67;
+---------+
| '87'+67 |
+---------+
| 154 |
+---------+
mysql> select 'hello'+21;
+------------+
| 'hello'+21 |
+------------+
| 21 |
+------------+
3.连接字符:concat
语法:select concat(str1,str2,…) from 表名;
**案例**
mysql>select concat(last_name,first_name) as 姓名 from employees;
mysql>SELECT CONCAT('hello','world');
+-------------------------+
| concat('hello','world') |
+-------------------------+
| helloworld |
+-------------------------+
**4.练习
显示出表 employees 的部分列,各个列之间用逗号连接,列头显示成 OUT_PUT **
**引号中的逗号,也会执行查询!**
mysql> select concat(first_name,',',last_name,',',job_id) as out_put
-> from employees;
+------------------------------+
| out_put |
+------------------------------+
| Steven,K_ing,AD_PRES |
| Neena,Kochhar,AD_VP |