2016.10.23 + 2016.11.02

1.选择所有数据(查看整表)

MariaDB [tianyuan]> select * from pet;
+----------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+-------+
4 rows in set (0.00 sec)

2.修改数据的二种办法

2.1 直接单处更新(优)

MariaDB [tianyuan]> UPDATE pet SET birth = '1994-04-12' WHERE name = 'Buffy';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [tianyuan]> select * from pet;
+----------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1994-04-12 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+-------+
4 rows in set (0.00 sec)

2.2 整表删除,重新加载

编辑文件"1.txt"改正错误,然后使用DELETE和LOAD DATA清空并重新装载表:

MariaDB [tianyuan]> DELETE FROM pet;
MariaDB [tianyuan]> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;

效果和上面一样,然而, 如果这样操做,必须重新输入Puffball记录。

2.选择特殊行

2.1 选择行

MariaDB [tianyuan]> select * from pet where name='Buffy';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1994-04-12 | NULL |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.2 活用WHERE

WHERE... : 指定条件(=, >, <)

MariaDB [tianyuan]> SELECT * FROM pet WHERE birth > '1994-4-12';
+-------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person | f | 1997-11-13 | NULL |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.3 WHERE...AND... : 组合条件

MariaDB [tianyuan]> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.4 WHERE...OR... : 组合条件

SELECT * FROM pet WHERE species = 'dog' OR species = 'cat';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
3 rows in set (0.00 sec)

2.5 WHERE...AND...OR... : 组合条件

AND和OR可以混用,但AND比OR具有更高的优先级。如果你使用两个操作符,使用圆括号指明如何对条件进行分组是一个好主意。

MariaDB [tianyuan]> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
-> OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)

3.选择特殊列

3.1 宠物的名字与出生日期列组合呈现

MariaDB [tianyuan]> SELECT name, birth FROM pet;
+--------+------------+
| name | birth |
+--------+------------+
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| yuner | 1997-11-13 |
+--------+------------+
4 rows in set (0.00 sec)

3.2 宠物的主人名单列呈现

MariaDB [tianyuan]> SELECT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Harold |
| ayuan |
+--------+
4 rows in set (0.00 sec)

3.3 Distinct: 上条名单去重效果

MariaDB [tianyuan]> SELECT DISTINCT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| ayuan |
+--------+
3 rows in set (0.00 sec)

3.4 WHERE子句结合行选择与列选择

MariaDB [tianyuan]> SELECT name, species, birth FROM pet
-> WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+
| name | species | birth |
+--------+---------+------------+
| Fluffy | cat | 1993-02-04 |
| Claws | cat | 1994-03-17 |
| Buffy | dog | 1989-05-13 |
+--------+---------+------------+
3 rows in set (0.00 sec)

4.分类行

4.1 排序(ORDER BY)

MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth;
+--------+------------+
| name | birth |
+--------+------------+
| Buffy | 1989-05-13 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| yuner | 1997-11-13 |
+--------+------------+
4 rows in set (0.06 sec)

4.2 降序(DESC)

MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth DESC;
+--------+------------+
| name | birth |
+--------+------------+
| yuner | 1997-11-13 |
| Claws | 1994-03-17 |
| Fluffy | 1993-02-04 |
| Buffy | 1989-05-13 |
+--------+------------+
4 rows in set (0.00 sec)

4.3 混合序

species升,birth降

MariaDB [tianyuan]> select name, species, birth FROM pet
-> ORDER BY species, birth DESC;
+--------+---------+------------+
| name | species | birth |
+--------+---------+------------+
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
| Buffy | dog | 1989-05-13 |
| yuner | person | 1997-11-13 |
+--------+---------+------------+
4 rows in set (0.06 sec)

species降,birth降

MariaDB [tianyuan]> select name, species, birth FROM pet
-> ORDER BY species DESC, birth DESC;
+--------+---------+------------+
| name | species | birth |
+--------+---------+------------+
| yuner | person | 1997-11-13 |
| Buffy | dog | 1989-05-13 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
+--------+---------+------------+
4 rows in set (0.00 sec)

5.日期计算

5.1 宠物年龄计算(简单的日期计算)

MariaDB [tianyuan]> SELECT name, birth, CURDATE(),
-> (YEAR(CURDATE())-YEAR(birth))
-> - (RIGHT(CURDATE(),5)<RIGHT(birth,5))
-> AS age
-> FROM pet;
+--------+---------+------------+
| name | species | birth |
+--------+---------+------------+
| yuner | person | 1997-11-13 |
| Buffy | dog | 1989-05-13 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
+--------+---------+------------+
4 rows in set (0.00 sec)
  • CURDATE():提前当前精确日期
  • YEAR():提取日期的年部分
  • RIGHT():提取日期的MM-DD (日历年)部分的最右面5个字符(即月与日)。
    • 比较MM-DD值的表达式部分的值一般为1或0,如果CURDATE()(今年)的月与日比birth(出生时)的月与日早,则年份应减去1(因为还没到达生日日期)。

5.2 已死亡宠物年龄计算(带条件的日期计算)

MariaDB [tianyuan]> select name, birth, death,
-> (YEAR(death)-YEAR(birth)) - (Right(death,5)<RIGHT(birth,5))
-> AS age
-> FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+
| name | birth | death | age |
+--------+------------+------------+------+
| Fluffy | 1993-02-04 | 1995-02-04 | 2 |
+--------+------------+------------+------+
1 row in set (0.03 sec)

5.3 哪个宠物下个月过生日

MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = 11;
+-------+------------+
| name | birth |
+-------+------------+
| yuner | 1997-11-13 |
+-------+------------+
1 row in set (0.00 sec)

5.3.1 疑问:11能不能换成本月+1?或者取余

MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+1);
Empty set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+MONTH(birth)/MONTH(birth));
Empty set (0.07 sec)

5.3.2 正解1

MariaDB [tianyuan]> SELECT name, birth FROM pet
-> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));

DATE_ADD( )允许在一个给定的日期上加上时间间隔

5.3.3 正解2

MariaDB [tianyuan]> SELECT name, birth FROM pet
-> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;

得到的结果是一致的,因为目的一样。

+-------+------------+
| name | birth |
+-------+------------+
| yuner | 1997-11-13 |
+-------+------------+
1 row in set (0.11 sec)

6.NULL值操作

6.1 算术比较操作符无效

MariaDB [tianyuan]> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 NULL | 1 NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+
1 row in set (0.00 sec)

6.2 IS NULL, IS NOT NULL

MariaDB [tianyuan]> SELECT 1 IS NULL, 1 IS NOT NULL, null IS NULL, null IS NOT NULL;
+-----------+---------------+--------------+------------------+
| 1 IS NULL | 1 IS NOT NULL | null IS NULL | null IS NOT NULL |
+-----------+---------------+--------------+------------------+
| 0 | 1 | 1 | 0 |
+-----------+---------------+--------------+------------------+
1 row in set (0.00 sec)

7.模式匹配

7.1 x%:以x开头的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE 'y%';
+-------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person | f | 1997-11-13 | NULL |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.2 %x:以x结尾的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%r';
+-------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person | f | 1997-11-13 | NULL |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.3 %x%:包含x的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%un%';
+-------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person | f | 1997-11-13 | NULL |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.4 x:正好包含x个字符的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '______';(6个下划线)
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | 1995-02-04 |
+--------+--------+---------+------+------------+------------+
1 row in set (0.00 sec)

7.5 MySQL正则表达式

操作中遇到了问题,暂时跳过,语法较为简单,只是需要练习。

MariaDB [tianyuan]> SELECT * FROM pet WHERE name REGEXP '^.{5}$';
ERROR 1139 (42000): Got error 'this version of PCRE is compiled
without UTF support at offset 0' from regexp

8.计数行

8.1 在pet表中有多少行?(动物数目)

MariaDB [tianyuan]> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)

8.2 每个主人有多少宠物(GROUP BY)

MariaDB [tianyuan]> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| ayuan | 1 |
| Gwen | 1 |
| Harold | 2 |
+--------+----------+
3 rows in set (0.00 sec)

8.3 每种动物的数量?

MariaDB [tianyuan]> SELECT species, COUNT(*) FROM pet GROUP BY owner;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| person | 1 |
| cat | 1 |
| cat | 2 |
+---------+----------+
3 rows in set (0.00 sec)

8.4 按种类和性别组合的动物数量

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| person | f | 1 |
+---------+------+----------+
4 rows in set (0.00 sec)

8.5 只对狗和猫进行检索时

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet
-> WHERE species = 'dog' OR species = 'cat'
-> GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
+---------+------+----------+
3 rows in set (0.00 sec)

8.6 已知性别的按性别的动物数目

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet                  -> WHERE sex IS NOT NULL
-> GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| person | f | 1 |
+---------+------+----------+
4 rows in set (0.00 sec)

9.使用1个以上的表

9.1 创建第二张表

MariaDB [tianyuan]> CREATE TABLE event (name VARCHAR(20), date DATE,
-> type VARCHAR(15), remark VARCHAR(255));
Query OK, 0 rows affected (0.10 sec)

9.2 装载event.txt文件

LOAD DATA LOCAL INFILE 'desktop/event.txt' INTO TABLE event;
MariaDB [tianyuan]> select * from event;
+--------+------------+--------+-----------------------------+
| name | date | type | remark |
+--------+------------+--------+-----------------------------+
| Fluffy | 1995-05-15 | litter | 4 kittens, 3 female, 1 male |
| Buffy | 1993-06-23 | litter | 5 puppies, 2 female, 3 male |
| Buffy | 1994-06-19 | litter | 3 puppies, 3 female |
| Chirpy | 1999-03-21 | vet | needed beak straightened |
| Slim | 1997-08-03 | vet | broken rib |
| Bowser | 1991-10-10 | kennel | NULL |
+--------+------------+--------+-----------------------------+
7 rows in set (0.00 sec)

9.3 根据需要来使用两张表的数据(组成新表呈现)

MariaDB [tianyuan]> SELECT pet.name,
-> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age,
-> remark
-> FROM pet, event
-> WHERE pet.name = event.name AND event.type = 'litter';
+--------+------+-----------------------------+
| name | age | remark |
+--------+------+-----------------------------+
| Fluffy | 2 | 4 kittens, 3 female, 1 male |
| Buffy | -24 | 5 puppies, 2 female, 3 male |
| Buffy | -23 | 3 puppies, 3 female |
+--------+------+-----------------------------+
3 rows in set (0.00 sec)

重拾SQL——表中索值的更多相关文章

  1. 重拾SQL——从无到有

    2016.10.22 因为工作需要,在这里提前重拾sql. 0.创建并选择数据库 mysql> SHOW DATABASES; +--------------------+ | Database ...

  2. oracle定时器,调用存储过程,定时从n张表中取值新增到本地一张表中

    --创建新增本地数据库的存储过程create or replaceprocedure pro_electric_record as  begin    insert into electric_met ...

  3. 已知长度为n的线性表采用顺序结构,写一算法删除该线性表中所有值为item的元素

    /** * @author:(LiberHome) * @date:Created in 2019/2/27 23:34 * @description: * @version:$ */ /*已知长度为 ...

  4. 【Transact-SQL】计算整个表中所有值的出现的次数

    原文:[Transact-SQL]计算整个表中所有值的出现的次数 一个表有3列,5行,那么一共有15个值,现在要计算整个表中所有值在表中出现的次数,不过这里表的列数是不确定的,上面的例子是3列,实际上 ...

  5. orcle数据库表中字段值含有单引号,如何模糊搜索?

    例如:T_table表中,name字段值为:字符串含有‘单引号’: SQL模糊搜索语句应该如下:select * from T_table where name like '%含有''单引号''%'

  6. 将表中null值替换成想要的值、查询某一列值为null

    用到ISNULL()函数 例如:SELECT 其他列名,ISNULL(列名,替换值)as 重命名  from 表名 (简单参考:http://www.cnblogs.com/netsa/archive ...

  7. 使用T2表中的值替换T1表的值

    描述:现在有两张表,T1由Key和Value两个字段,T2也有Key和Value两个字段 当T1中的Key在T2表中存在时,更新使用T2表中对用的Value 值替换T1中的VAlue update A ...

  8. 以A表中的值快速更新B表中记录的方法

    1.问题描述 有两张表,A表记录了某些实体的新属性,B表记录了每个实体的旧属性,现在打算用A中的属性值去更新B中相同实体的旧属性,如下图所示: 类似这样的需求,怎样做比较高效呢? 2.制作模拟数据   ...

  9. asp.net c#整理所有本地的图片一次性保存到SQL表中

    string sql1 = "select distinct tx from tiku where tx is not null"; //检索tx表中所有的不重复的tx值 stri ...

随机推荐

  1. GUI学习之二十三——QComboBox学习总结

    我们在前面分别介绍了两种输入控件:纯键盘文本输入和步长调节器,下面我们来学习下组合框(下拉选择输入). 一.简介 1.下拉框是一个组合控件(包含一个文本显示控件和一个按钮).它默认显示最小的控件给用户 ...

  2. pandas的数据统计函数

    # 1汇总类统计 # 2唯一去重和按值计数 # 3 相关系数和协方差 import pandas as pd # 0 读取csv数据 df = pd.read_csv("beijing_ti ...

  3. 九、ARM 汇编与 C 的混合编程

    9.1 ARM 汇编与 C 的混合编程 9.1.1 内嵌汇编 __asm __asm("指令")例如关闭/打开总中断开关 CPSR __asm //使用 C 中变量名代替寄存器 { ...

  4. Deepin(Ubuntu)安装rpm软件包

    1.首先安装alien和fakeroot这两个软件,alien可以将rpm转换为deb包. 在终端中输入命令 sudo apt-get install alien fakeroot 2.使用alien ...

  5. LOJ6279 果树

    我丢 之前sun在某校集训给我看过 当时也没想起来 今天补省集的锅的时候发现 wok这题我还听过?! 身败名裂.jpg (可是你记性不好这事情不已经人尽皆知了吗? 咳咳 回归正题 考虑对于两个同色的点 ...

  6. 【NOIP2016A组模拟7.13】亚瑟王之宫

    题目 分析 我们定义\(dis_{x,y,x1,y2}\)表示\((x,y)\)到\((x1,y1)\)的距离.这个用spfa求. 接着,枚举两个集合点\((x,y).(x1,y1)\), 得出这两个 ...

  7. linux运维、架构之路-cobbler无人值守

    一.cobbler介绍 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等 1.主要功能:①Cobbl ...

  8. 如何使 C++ 的 StringBuilder 提升 4350% 的性能?

    介绍 经常出现客户端打电话抱怨说:你们的程序慢如蜗牛.你开始检查可能的疑点:文件IO,数据库访问速度,甚至查看web服务. 但是这些可能的疑点都很正常,一点问题都没有. 你使用最顺手的性能分析工具分析 ...

  9. Web防止button按钮点击多次

    BtnPass.Attributes.Add("onclick", "javascript:if(document.getElementById('IsSubmited' ...

  10. Java——动态绑定和多态

    动态绑定是在执行期间(而不是编译期间)判断所用对象的实际类型,根据其实际的类型调用其相应的方法(确定选择哪个多态形式被调用). 首先是方法的参数是父类对象,传入子类对象是否可行 然后引出Parent ...