码迷,mamicode.com
首页 > 其他好文 > 详细

select

时间:2018-08-26 16:45:06      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:sql   通配   字符串匹配   iad   连接字符串   count   des   建表   模糊查询   

单表查询

测试表:company库.employee员工5
雇员编号 id int
雇员姓名 name varchar(30)
雇员性别 sex enum
雇用时期 hire_date date
职位 post varchar(50)
职位描述 job_description varchar(100)
薪水 salary double(15,2)
办公室 office int
部门编号 dep_id int

用edit---->复制粘贴保存
;

操作如下:
1.mysql<edit 回车
2.把下面的命令复制粘贴
3.保存退出然后iu打分号; 回车
4.再相同操作粘贴复制下面(插入记录的那些命令)

创建表
mysql> CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum(‘male‘,‘female‘) default ‘male‘ not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);

插入记录
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
(‘jack‘,‘male‘,‘20180202‘,‘instructor‘,‘teach‘,5000,501,100),
(‘tom‘,‘male‘,‘20180203‘,‘instructor‘,‘teach‘,5500,501,100),
(‘robin‘,‘male‘,‘20180202‘,‘instructor‘,‘teach‘,8000,501,100),
(‘alice‘,‘female‘,‘20180202‘,‘instructor‘,‘teach‘,7200,501,100),
(‘tianyun‘,‘male‘,‘20180202‘,‘hr‘,‘hrcc‘,600,502,101),
(‘harry‘,‘male‘,‘20180202‘,‘hr‘,NULL,6000,502,101),
(‘emma‘,‘female‘,‘20180206‘,‘sale‘,‘salecc‘,20000,503,102),
(‘christine‘,‘female‘,‘20180205‘,‘sale‘,‘salecc‘,2200,503,102),
(‘zhuzhu‘,‘male‘,‘20180205‘,‘sale‘,NULL,2200,503,102),
(‘gougou‘,‘male‘,‘20180205‘,‘sale‘,‘‘,2200,503,102);

======================================
开始:
mysql> select 字段名称,字段名称2 from 表名 条件

简单查询:
mysql> select * from t3;
mysql> select name , salary薪水, dep_id from employee5;
mysql> select name as mingzi, salary薪水 as moeny , dep_id from employee5;
mysql> select name as mingzi, salary+dep_id as mun from employee5;
as可以不写

避免重复DISTINCT
SELECT post FROM employee5;
SELECT DISTINCT post FROM employee5; ---去重
注:不能部分使用DISTINCT,通常仅用于某一字段。

通过四则运算查询
shell中调用mysql:第一种方法:mysql -e "select 32747*43868" | tail -1
第二种方法:
mysql <<eof

eof

运算:
mysql>select math*china-50 from t1;
mysql>select 437.4384/5
mysql>select 5>3;  (true为1)

SELECT name, salary, salary*14 FROM employee5;
SELECT name, salary, salary*14 AS Annual_salary FROM employee5;
SELECT name, salary, salary*14 Annual_salary FROM employee5;

定义显示格式
CONCAT() 函数用于连接字符串(连接在一起)
SELECT CONCAT(name, ‘ annual salary: ‘, salary*14) AS Annual_salary FROM employee5;

单条件查询
mysql> select pass from t3 where name="wing";
where not name= "wing" 除了wing的

多条件查询
mysql>select math from db1.t1 where math>50 and math<600;and也可以是&&
mysql>select math from db1.t1 where not math>50;

关键字BETWEEN AND
    SELECT name,salary FROM employee5 
        WHERE salary BETWEEN 5000 AND 15000;

    SELECT name,salary FROM employee5 
        WHERE salary NOT BETWEEN 5000 AND 15000;

关键字IS NULL
    SELECT name,job_description FROM employee5 
        WHERE job_description IS NULL;

    SELECT name,job_description FROM employee5 
        WHERE job_description IS NOT NULL;

    SELECT name,job_description FROM employee5 
        WHERE job_description=‘‘;

    NULL说明:
    1、等价于没有任何值、是未知数。
   2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。
    3、对空值做加、减、乘、除等运算操作,结果仍为空。
    4、比较时使用关键字用“is null”和“is not null”。
    5、排序时比其他数据都小(索引默认是降序排列,小→大),所以NULL值总是排在最前。

关键字IN集合查询
    SELECT name, salary FROM employee5 
        WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ;

    SELECT name, salary FROM employee5 
        WHERE salary IN (4000,5000,6000,9000) ;

    SELECT name, salary FROM employee 
        WHERE salary NOT IN (4000,5000,6000,9000) ;

排序查询

mysql> select china from t1 order by china;
mysql> select china from t1 order by china desc;  降序
mysql> select china from t1 order by china desc limit 3;  降序前三名
mysql> select china from t1 order by china desc limit 1,3;从第2行开始取三个

注:
ascending    美音 /?‘s?nd??/   升序(默认)
descending  美音 /d?‘s?nd??/  降序

按多列排序:
    入职时间相同的人薪水不同
    SELECT * FROM employee5 
        ORDER BY hire_date DESC, 
        salary ASC;

先按入职时间,再按薪水排序

先按职位,再按薪水排序

限制查询的记录数
    SELECT * FROM employee5 ORDER BY salary DESC 
        LIMIT 5;                            //默认初始位置为0 

    SELECT * FROM employee5 ORDER BY salary DESC
        LIMIT 0,5;

    SELECT * FROM employee5 ORDER BY salary DESC
        LIMIT 3,5;                          //从第4条开始,共显示5条

开始:
分组查询
mysql> select count(gender),gender from t3 group by gender;
统计男女各多少人

GROUP BY和GROUP_CONCAT()函数一起使用(把不同组的记录放到一行)
SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id;
SELECT dep_id,GROUP_CONCAT(name) as emp_members FROM employee5 GROUP BY dep_id;

GROUP BY和集合函数SUM(salary)一起使用   -------计算每个部门总共多少钱

模糊查询(通配符)
_ 任意单个字符

%  所有字符

mysql> select * from t1 where china=‘1__‘;
mysql> select * from t1 where china like ‘%0%‘;

正则查询
mysql> select from t1 where china regexp ‘10+‘;
SELECT
FROM employee5 WHERE name REGEXP ‘^ali‘;
SELECT FROM employee5 WHERE name REGEXP ‘yun$‘;
SELECT
FROM employee5 WHERE name REGEXP ‘m{2}‘;

子查询
mysql> select name from t2 where math=(select max(math) from t2);

函数
count()
max()
min()
avg()
database()
user()
now()
sum()

password()
md5()
sha1()
power()

SELECT COUNT(*) FROM employee5;
SELECT COUNT(*) FROM employee5 WHERE dep_id=101;
SELECT MAX(salary) FROM employee5;
SELECT MIN(salary) FROM employee5;
SELECT AVG(salary) FROM employee5;
SELECT SUM(salary) FROM employee5;
SELECT SUM(salary) FROM employee5 WHERE dep_id=101; 

MariaDB [company]> select password(5);
+-------------------------------------------+
| password(5)                               |
+-------------------------------------------+
| *7534F9EAEE5B69A586D1E9C1ACE3E3F9F6FCC446 |
+-------------------------------------------+

MariaDB [company]> select md5(5);
+----------------------------------+
| md5(5)                           |
+----------------------------------+
| e4da3b7fbbce2345d7772b0674a318d5 |
+----------------------------------+
1 row in set (0.00 sec)

MariaDB [company]> select sha1(5);
+------------------------------------------+
| sha1(5)                                  |
+------------------------------------------+
| ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4 |
+------------------------------------------+
1 row in set (0.00 sec)

POWER() function
MySQL POWER() returns the value of a number raised to the power of another number. The synonym of POWER() is POW().

mysql> SELECT POWER(3, 2);
+-------------+
| POWER(3, 2) |
+-------------+
|           9 | 
+-------------+
1 row in set (0.00 sec)

小结:对字符串匹配的方式
WHERE name = ‘tom‘;
WHERE name LIKE ‘to%‘;
WHERE name REGEXP ‘yun$‘;

select

标签:sql   通配   字符串匹配   iad   连接字符串   count   des   建表   模糊查询   

原文地址:http://blog.51cto.com/13939728/2164515

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