SQL编程总结
作者:互联网
1、查找
SELECT * FROM Websites;
SELECT DISTINCT country FROM Websites; //选取唯一不同值
SELECT * FROM Websites WHERE country='CN';
= 等于
<> 不等于。注释:在 SQL 的一些版本中,该操作符可被写成 !=
> 大于
< 小于
>= 大于等于
<= 小于等于
BETWEEN 在某个范围内
LIKE 搜索某种模式
IN 指定针对某个列的多个可能值
AND OR
SELECT * FROM Websites ORDER BY alexa DESC;
SELECT * FROM Websites ORDER BY country,alexa;
2、插入
INSERT INTO Websites (name, url, alexa, country)
VALUES ('百度','https://www.baidu.com/','4','CN');
3、更新
UPDATE Websites
SET alexa='5000', country='USA'
WHERE name='菜鸟教程';
4、删除
DELETE FROM Websites
WHERE name='百度' AND country='CN';
5、查找属性
SELECT * FROM Websites LIMIT 2 OFFSET 2;
SELECT * FROM Websites WHERE name LIKE '%oo%';
% 替代一个或多个字符
_ 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist]或者[!charlist] 不在字符列中的任何单一字符
SELECT * FROM Websites WHERE name REGEXP '^[A-H]';
SELECT * FROM Websites WHERE name IN ('Google','菜鸟教程');
SELECT * FROM Websites WHERE (alexa BETWEEN 1 AND 20) AND country NOT IN ('USA', 'IND');
SELECT name AS n, country AS c FROM Websites;
SELECT w.name, w.url, a.count, a.date FROM Websites AS w, access_log AS a WHERE a.site_id=w.id and w.name="菜鸟教程";
6、连接
INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
7、SQL函数
AVG() - 返回平均值
COUNT() - 返回行数 SELECT COUNT(DISTINCT site_id) AS nums FROM access_log;
FIRST() - 返回第一个记录的值
LAST() - 返回最后一个记录的值
MAX() - 返回最大值
MIN() - 返回最小值
SUM() - 返回总和
UCASE() - 将某个字段转换为大写
LCASE() - 将某个字段转换为小写
MID() - 从某个文本字段提取字符,MySql 中使用
SubString(字段,1,end) - 从某个文本字段提取字符
LEN() - 返回某个文本字段的长度
ROUND() - 对某个数值字段进行指定小数位数的四舍五入
NOW() - 返回当前的系统日期和时间
FORMAT() - 格式化某个字段的显示方式
GROUP BY
SELECT site_id, SUM(access_log.count) AS nums
FROM access_log GROUP BY site_id;
SELECT Websites.name,COUNT(access_log.aid) AS nums FROM access_log
LEFT JOIN Websites
ON access_log.site_id=Websites.id
GROUP BY Websites.name;
HAVING (HAVING 子句可以让我们筛选分组后的各组数据)
SELECT Websites.name, SUM(access_log.count) AS nums FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id
WHERE Websites.alexa < 200
GROUP BY Websites.name
HAVING SUM(access_log.count) > 200;
标签:总结,name,country,编程,Websites,access,SQL,SELECT,log 来源: https://www.cnblogs.com/share-ideas/p/11260618.html