学了一个月的java,开始有入门的感觉。这段时间接触到了java的JDBC, 发现学习这部分的内容还是要有SQL的基础,于是花费了几天时间学习了一下SQL语法,并将其总结于下。
选择数据 SELECT * FROM table;
排序数据 SELECT [a,b,c] FROM table ORDER BY [a,b]; //先按照a列排序,相同的再按照b列排序
过滤数据 SELECT [a,b] FROM table WHERE condition;
高级过滤 SELECT [a,b] FROM table WHERE condition1 AND/OR condition2;
通配符过滤 SELECT [a,b] FROM table WHERE a LIKE 'fish%'; // [%],[_],[[]]三个通配符
计算字段 SELECT a + '('+b+')' FROM table ORDER BY a; //拼接
SELECT RTRIM(a)+'('RTRIM(b)')' AS c FROM table ORDER BY a; //列别名
函数字段 SELECT [a,b] FROM table WHERE SOUNDEX(a) = SOUNDEX('tim'); //根据发音匹配
汇总数据 SELECT AVG(a) AS c FROM table WHERE condition; // COUNT(), MAX(), MIN(), SUM()汇总函数
分组数据 SELECT a, COUNT() AS c FROM table GROUP BY a; // 创建分组
SELECT a, COUNT() AS c FROM table GROUP BY a HAVING condition; // 过滤分组
SELECT - FROM - WHERE - GROUP BY - HAVING - ORDER BY // 分组语句的关键词顺序
组合查询 SELECT [a,b] FROM table WHERE c IN('MI') UNION SELECT [a,b] FROM table WHERE condition;
插入数据 INSERT INTO table VALUES('','',''...);
INSERT INTO table2(a,b,c) SELECT a,b,c FROM table1; // 插入检索的数据
SELECT * INTO table2 FROM table1; // 复制表
更新数据 UPDATE table SET a='' WHERE condition;
删除数据 DELETE FROM table WHERE condition;
创建表 CREATE TABLE table {...};
更新表 ALTER TABLE table ADD a CHAR(20);
删除表 DROP TABLE table;
注: 本内容总结自《SQL必知必会》
新手了解SQL推荐codecademy网站