SQL-DQL

我们在MySQL和Spark-SQL使用过程中,常用的DQL如下。

// 查询
SELECT xx FROM table;
SELECT * FROM table;
SELECT DISTINCT xx FROM table;

// + - * / 运算
SELECT xx*1.5 FROM table;
SELECT xx+xx FROM table;

// 条件查询
SELECT xx FROM table WHERE xx> 10000 AND xx IS NOT NULL;
SELECT xx FROM table WHERE xx BETWEEN 100 AND 300;
SELECT xx FROM table  WHERE xx IN ('xx', 'xxx');

// 模糊查询
SELECT * FROM table WHERE xx LIKE 'M_';   // 包含M的两个字
SELECT * FROM table WHERE xx LIKE '___';   // 包含三个字
SELECT * FROM table WHERE xx LIKE 'M%';   // 前面包含M字符的所有内容
SELECT * FROM table WHERE xx LIKE '%M%';   // 中间包含M字符的所有内容
SELECT * FROM table WHERE xx LIKE '%';   // 包含除过NULL以外的所有内容

// 排序
SELECT * FROM  table ORDER BY xx ASC;   // 升序
SELECT * FROM  table ORDER BY xx DESC;   // 降序
SELECT * FROM  table ORDER BY xx ASC, xx DESC; // 多列排序

// 聚合函数
SELECT COUNT(*) FROM table WHERE xx>100;
SELECT MAX(xx) FROM table;
SELECT MIN(xx) FROM table;
SELECT SUM(xx) FROM table;
SELECT AVG(xx) FROM table;

// 分组查询
SELECT xx, COUNT(*) FROM table GROUP BY xx; // 
SELECT xx,COUNT(*) FROM table GROUP BY xx ORDER BY COUNT(*) DESC; // 对xx进行TOP排序

// 显示这两张表中记录数的总和
https://jingyan.baidu.com/article/fdbd4277a4f9ebb89f3f4878.html
select t1.num1+t2.num2 from 
  (select count(*) num1 from table1) t1,
  (select count(*) num2 from table2) t2

 

你可能感兴趣的:(大数据,SQL)