《MySQL数据库》MySQL常用语法(二)

表关联关系:

-- 内联接
SELECT * FROM m INNER JOIN n ON m.id = n.id;
-- 左外联接
SELECT * FROM m LEFT JOIN n ON m.id = n.id;
-- 右外联接
SELECT * FROM m RIGHT JOIN n ON m.id = n.id;
-- 交叉联接(基本禁止写法,除非特殊情况)
SELECT * FROM m CROSS JOIN n;   -- 标准写法
SELECT * FROM m, n;
-- 要求UNION ALL 上下ssql字段要一致,将上下sql的记录拼在一起。
-- union 会过滤重复记录,union all 不会过滤。
SELECT id,name FROM m
UNION ALL
SELECT id,name FROM n;

常用函数:

-- 聚合函数(字段取别名可以省略 AS)
SELECT count(1) AS counts FROM XXX;   -- 查询记录条数
SELECT sum(age) AS sumAge FROM XXX;   -- 总和(全部记录age字段的和)
SELECT avg(age) AS avgAge FROM XXX;   -- 平均值(全部记录age字段的平均值)
SELECT max(age) AS maxAge FROM XXX;   -- 最大值(全部记录age字段的最大值)
SELECT min(age) AS minAge FROM XXX;   -- 最小值(全部记录age字段的最小值)
-- 实际聚合函数使用场景(按照“字段” 分组,聚合函数可以按照分组聚合),having 可以增加条件去掉不需要的分组。
select count(1) from XXX GROUP BY 字段 HAVING 条件(比如字段=1-- 数学函数
SELECT abs(-5);   -- 绝对值
SELECT bin(15), oct(15), hex(15);   -- 二进制,八进制,十六进制
SELECT pi();   -- 圆周率3.141593
SELECT ceil(5.5);   -- 大于x的最小整数值6
SELECT floor(5.5);   -- 小于x的最大整数值5
SELECT greatest(3,1,4,1,5,9,2,6);   -- 返回集合中最大的值9
SELECT least(3,1,4,1,5,9,2,6);    -- 返回集合中最小的值1
SELECT mod(5,3);    -- 余数2
SELECT rand();    -- 返回0到1内的随机小数,每次不一样
SELECT rand(4);   -- 提供一个参数(种子)使RAND()随机数生成器生成一个指定的值。
SELECT round(1415.1415);   -- 四舍五入1415
SELECT round(1415.1415, 3);   -- 四舍五入三位数1415.142
SELECT round(1415.1415, -1);    -- 四舍五入整数位数1420
SELECT truncate(1415.1415, 3);    -- 截短为3位小数1415.141
SELECT truncate(1415.1415, -1);   -- 截短为-1位小数1410
SELECT sign(-5);    -- 符号的值负数-1
SELECT sign(5);    -- 符号的值正数1
SELECT sqrt(9);   -- 平方根3
-- 字符串函数
SELECT concat('a', 'p', 'p', 'le');   -- 连接字符串-apple
SELECT concat_ws(',', 'a', 'p', 'p', 'le');   -- 连接用','分割字符串-a,p,p,le
SELECT insert('chinese', 3, 2, 'IN');    -- 将字符串'chinese'从3位置开始的2个字符替换为'IN'-chINese
SELECT left('chinese', 4);   -- 返回字符串'chinese'左边的4个字符-chin
SELECT right('chinese', 3);   -- 返回字符串'chinese'右边的3个字符-ese
SELECT substring('chinese', 3);   -- 返回字符串'chinese'第三个字符之后的子字符串-inese
SELECT substring('chinese', -3);   -- 返回字符串'chinese'倒数第三个字符之后的子字符串-ese
SELECT substring('chinese', 3, 2);   -- 返回字符串'chinese'第三个字符之后的两个字符-in
SELECT trim(' chinese ');    -- 切割字符串' chinese '两边的空字符-'chinese'
SELECT ltrim(' chinese ');    -- 切割字符串' chinese '两边的空字符-'chinese '
SELECT rtrim(' chinese ');    -- 切割字符串' chinese '两边的空字符-' chinese'
SELECT repeat('boy', 3);    -- 重复字符'boy'三次-'boyboyboy'
SELECT reverse('chinese');    -- 反向排序-'esenihc'
SELECT length('chinese');   -- 返回字符串的长度-7
SELECT upper('chINese'), lower('chINese');    -- 大写小写 CHINESE    chinese
SELECT ucase('chINese'), lcase('chINese');    -- 大写小写 CHINESE    chinese
SELECT position('i' IN 'chinese');    -- 返回'i'在'chinese'的第一个位置-3
SELECT position('e' IN 'chinese');    -- 返回'i'在'chinese'的第一个位置-5
SELECT strcmp('abc', 'abd');    -- 比较字符串,第一个参数小于第二个返回负数- -1
SELECT strcmp('abc', 'abb');    -- 比较字符串,第一个参数大于第二个返回正数- 1
-- 时间函数
SELECT current_date, current_time, now();    -- 2018-01-13   12:33:43    2018-01-13 12:33:43
SELECT hour(current_time), minute(current_time), second(current_time);    -- 12  31   34
SELECT year(current_date), month(current_date), week(current_date);   -- 2018    1   1
SELECT quarter(current_date);   -- 1
SELECT monthname(current_date), dayname(current_date);   -- January  Saturday
SELECT dayofweek(current_date), dayofmonth(current_date), dayofyear(current_date);    -- 7   13  13
-- 控制流函数
SELECT if(3>2, 't', 'f'), if(3<2, 't', 'f');    -- t f
SELECT ifnull(NULL, 't'), ifnull(2, 't');    -- t 2
SELECT isnull(1), isnull(1/0);    -- 0 1 是null返回1,不是null返回0
SELECT nullif('a', 'a'), nullif('a', 'b');    -- null a 参数相同或成立返回null,不同或不成立则返回第一个参数
SELECT CASE 2
       WHEN 1 THEN 'first'
       WHEN 2 THEN 'second'
       WHEN 3 THEN 'third'
       ELSE 'other'
       END ;     -- second

常用系统表:

-- 查看数据库实例信息,“schema_name”数据库名称条件。
SELECT * from information_schema.`SCHEMATA` where schema_name = 'work_db'; 
-- 查看数据库表信息,“table_schema”数据库名称条件。
SELECT * from information_schema.`TABLES` where table_schema = 'work_db'; 
-- 查看数据库表列的信息,“table_schema”数据库名称条件,“table_name”查询表的条件。
SELECT * from information_schema.`COLUMNS` where table_schema = 'work_db' and table_name = 'operator';
-- 查看数据库表索引的信息,“table_schema”数据库名称条件,“table_name”查询表的条件。
SELECT * from information_schema.`STATISTICS` where table_schema = 'work_db' and table_name = 'operator';

其他信息:

-- 系统信息函数
SELECT database();    -- 当前数据库名-test
SELECT connection_id();   -- 当前用户id-306
SELECT user();    -- 当前用户-root@localhost
SELECT version();   -- 当前mysql版本
SELECT found_rows();    -- 返回上次查询的检索行数

参考:https://blog.csdn.net/c361604199/article/details/79479398

This moment will nap, you will have a dream; But this moment study,you will interpret a dream.
原文地址:https://www.cnblogs.com/jssj/p/11801259.html