1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| # 获取所有数据 SELECT * FROM pet;
# 更新某一条数据 UPDATE pet SET birth='1980-09-09' WHERE name='petpet';
# 根据限定条件搜索数据 # 字符串匹配 SELECT * FROM pet WHERE name='Puffball'; SELECT * FROM pet WHERE name<>'Puffball'; # 日期比较 SELECT * FROM pet WHERE birth>'1990-01-01'; # 逻辑运算 SELECT * FROM pet WHERE name='Puffball' AND birth>='1990-01-01'; SELECT * FROM pet WHERE name='Puffball' OR birth<='1990-01-01'; SELECT * FROM pet WHERE (name='Puffball' AND birth>='1990-01-01') OR (name='petpet' OR birth<='1990-01-01'); SELECT * FROM pet WHERE death IS NOT NULL;
# 仅选定指定词条 SELECT name FROM pet; SELECT name,owner FROM pet; SELECT species,sex FROM pet; # +---------+------+ # | species | sex | # +---------+------+ # | hamster | f | # | dog | f | # | bird | m | # | hamster | f | # +---------+------+ # 限定不重复词条 SELECT DISTINCT species,sex FROM pet; # +---------+------+ # | species | sex | # +---------+------+ # | hamster | f | # | dog | f | # | bird | m | # +---------+------+ # 3 rows in set (0.01 sec) SELECT DISTINCT species,DISTINCT sex FROM pet;
# 结果排序 # 排序中的比较和 WHERE 语句中的比较一样,是大小写不敏感的 # 可以强制使用 BINARY 关键字使其大小写敏感 # 使用 DESC (descending)关键字降序排列(默认是升序排列) SELECT name,birth FROM pet; # +----------+------------+ # | name | birth | # +----------+------------+ # | Puffball | 1999-03-30 | # | petpet | 1980-09-30 | # | Milk | 1949-03-30 | # | Pure | 1999-03-30 | # +----------+------------+ # 4 rows in set (0.00 sec) SELECT name,birth FROM pet ORDER BY birth; # +----------+------------+ # | name | birth | # +----------+------------+ # | Milk | 1949-03-30 | # | petpet | 1980-09-30 | # | Puffball | 1999-03-30 | # | Pure | 1999-03-30 | # +----------+------------+ # 4 rows in set (0.00 sec) SELECT name,birth FROM pet ORDER BY BINARY name; # +----------+------------+ # | name | birth | # +----------+------------+ # | Milk | 1949-03-30 | # | Puffball | 1999-03-30 | # | Pure | 1999-03-30 | # | petpet | 1980-09-30 | # +----------+------------+ # 4 rows in set, 1 warning (0.00 sec) # 降序排列 SELECT name,birth FROM pet ORDER BY birth DESC; # +----------+------------+ # | name | birth | # +----------+------------+ # | Puffball | 1999-03-30 | # | Pure | 1999-03-30 | # | petpet | 1980-09-30 | # | Milk | 1949-03-30 | # +----------+------------+ # 4 rows in set (0.00 sec) # 多重排序 SELECT name,sex,birth FROM pet ORDER BY sex,birth; # +----------+------+------------+ # | name | sex | birth | # +----------+------+------------+ # | petpet | f | 1980-09-30 | # | Puffball | f | 1999-03-30 | # | Pure | f | 1999-03-30 | # | Milk | m | 1949-03-30 | # +----------+------+------------+ # 4 rows in set (0.00 sec)
# 计算属性,使用 CURDATE() 获取当前日期,TIMESTAMPDIFF 计算日期差 # TIMESTAMPDIFF 首个参数是单位,同时可以使用 AS 关键字指定别名 SELECT name, birth, CURDATE(), TIMESTAMPDIFF(YEAR, birth, CURDATE()) AS age FROM pet; # +----------+------------+------------+------+ # | name | birth | CURDATE() | age | # +----------+------------+------------+------+ # | Puffball | 1999-03-30 | 2022-11-20 | 23 | # | petpet | 1980-09-30 | 2022-11-20 | 42 | # | Milk | 1949-03-30 | 2022-11-20 | 73 | # | Pure | 1999-03-30 | 2022-11-20 | 23 | # +----------+------------+------------+------+ # 4 rows in set (0.00 sec) # 注意其中 NULL 是无法用比较运算符的(包括<>),所以只能用IS NOT SELECT name, birth, death, TIMESTAMPDIFF(YEAR, birth, death) AS age FROM pet WHERE (birth IS NOT NULL) AND (death IS NOT NULL); # +------+------------+------------+------+ # | name | birth | death | age | # +------+------------+------------+------+ # | Milk | 1949-03-30 | 2009-04-04 | 60 | # +------+------------+------------+------+ # 1 row in set (0.00 sec)
# 计算日期的年月日 SELECT name, birth, YEAR(birth), MONTH(birth), DAYOFMONTH(birth) FROM pet; # +----------+------------+-------------+--------------+-------------------+ # | name | birth | YEAR(birth) | MONTH(birth) | DAYOFMONTH(birth) | # +----------+------------+-------------+--------------+-------------------+ # | Puffball | 1999-03-30 | 1999 | 3 | 30 | # | petpet | 1980-09-30 | 1980 | 9 | 30 | # | Milk | 1949-03-30 | 1949 | 3 | 30 | # | Pure | 1999-03-30 | 1999 | 3 | 30 | # +----------+------------+-------------+--------------+-------------------+ # 4 rows in set (0.00 sec)
# 计算出的年月日也可以参加条件运算 SELECT name, birth, MONTH(birth) FROM pet WHERE MONTH(birth)=9; # +--------+------------+--------------+ # | name | birth | MONTH(birth) | # +--------+------------+--------------+ # | petpet | 1980-09-30 | 9 | # +--------+------------+--------------+ # 1 row in set (0.00 sec)
# 查找下一个月生日的词条 SELECT name, birth FROM pet WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH)); # 另一种写法 # +1 是防止12月直接mod成0 SELECT name, birth FROM pet WHERE MONTH(birth) = MOD(MONTH(CURDATE()),12)+1; # +--------+------------+ # | name | birth | # +--------+------------+ # | ddddog | 2009-12-12 | # +--------+------------+ # 1 row in set (0.00 sec)
# 通配符 # 通配符表达式默认是大小写不敏感的 # 使用 _ 匹配单个字符,使用 % 匹配任意个字符(包括0个) # 使用 LIKE 和 NOT LIKE 连接 SELECT * FROM pet WHERE name LIKE 'p%'; # +----------+------------+---------+------+------------+-------+ # | name | owner | species | sex | birth | death | # +----------+------------+---------+------+------------+-------+ # | Puffball | Diane | hamster | f | 1999-03-30 | NULL | # | petpet | ownerowner | dog | f | 1980-09-30 | NULL | # | Pure | Ddddiane | hamster | f | 1999-03-30 | NULL | # +----------+------------+---------+------+------------+-------+ # 3 rows in set (0.00 sec) SELECT * FROM pet WHERE name LIKE '%u__'; # +------+----------+---------+------+------------+-------+ # | name | owner | species | sex | birth | death | # +------+----------+---------+------+------------+-------+ # | Pure | Ddddiane | hamster | f | 1999-03-30 | NULL | # +------+----------+---------+------+------------+-------+ # 1 row in set (0.00 sec)
# 正则表达式通配符 SELECT * FROM pet WHERE REGEXP_LIKE(name, '^pet'); # +--------+------------+---------+------+------------+-------+ # | name | owner | species | sex | birth | death | # +--------+------------+---------+------+------------+-------+ # | petpet | ownerowner | dog | f | 1980-09-30 | NULL | # +--------+------------+---------+------+------------+-------+ # 1 row in set (0.01 sec) # 强制使用大小写敏感匹配 # 不知道啥方法-好像不好使-使用c控制符 SELECT * FROM pet WHERE REGEXP_LIKE(name, '^p' COLLATE utf8mb4_0900_as_cs); # SELECT * FROM pet WHERE REGEXP_LIKE(name, (BINARY '^p')); SELECT * FROM pet WHERE REGEXP_LIKE(name, '^p', 'c');
# 行统计 # 统计所有行 SELECT COUNT(*) FROM pet; # +----------+ # | COUNT(*) | # +----------+ # | 5 | # +----------+ # 1 row in set (0.00 sec) # 分组统计 SELECT sex, COUNT(*) FROM pet GROUP BY sex; # +------+----------+ # | sex | COUNT(*) | # +------+----------+ # | f | 4 | # | m | 1 | # +------+----------+ # 2 rows in set (0.00 sec) SELECT sex, COUNT(sex) FROM pet GROUP BY sex;
# 创建新表 event create table event (name VARCHAR(20), date DATE, type VARCHAR(15), remark VARCHAR(255)); insert into event value('Milk', '1966-03-04', 'litter','no'); insert into event value('Pure', '2008-05-04', 'litter','nonono');
# 同时操作两个表 # 将两个表正交,并选择 pet.name=event.name 的词条 # 其中 ON 起到了 WHERE 的作用 # 同样可以在后面追加 WHERE 语句 SELECT pet.name, TIMESTAMPDIFF(YEAR, birth, date) as age, remark FROM pet INNER JOIN event ON pet.name=event.name AND event.type='litter'; # WHERE event.type='litter'; # +------+------+--------+ # | name | age | remark | # +------+------+--------+ # | Milk | 16 | no | # | Pure | 9 | nonono | # +------+------+--------+ # 2 rows in set (0.00 sec) SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species FROM pet AS p1 INNER JOIN pet AS p2 ON p1.species = p2.species AND p1.sex = 'f' AND p1.death IS NULL AND p2.sex = 'f' AND p2.death IS NULL; # +----------+------+----------+------+---------+ # | name | sex | name | sex | species | # +----------+------+----------+------+---------+ # | Pure | f | Puffball | f | hamster | # | Puffball | f | Puffball | f | hamster | # | petpet | f | petpet | f | dog | # | Pure | f | Pure | f | hamster | # | Puffball | f | Pure | f | hamster | # | ddddog | f | ddddog | f | birrrrd | # +----------+------+----------+------+---------+ # 6 rows in set (0.00 sec)
|