学习sql的基本查询。
语法:
SELECT
column_1, column_2, ...
FROM
table_1
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
查询多列数据:
SELECT
lastName,firstName,jobTitle
FROM
employees;
查询所有数据:
SELECT * FROM employees;
条件查询:
SELECT
lastName,firstName,jobTitle
FROM
employees
WHERE
jobTitle = 'Sales Rep';
语法:
DELETE FROM table_name
WHERE condition;
删除所有officeNumber为4的员工:
DELETE FROM employees
WHERE officeCode = 4;
GROUP BY 语句根据一个或多个列对结果集进行分组。在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。
统计同一条产品线上生产多少种产品:
SELECT productLine,COUNT(productLine) FROM products GROUP BY productLine HAVING COUNT(productLine)>1;
我们知道从 MySQL 表中使用 SQL SELECT 语句来读取数据。如果我们需要对读取的数据进行排序,我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。
按价格降序排序:
SELECT * From products ORDER BY buyPrice DESC LIMIT 10;
MySQL 字符串函数: CONCAT(s1,s2...sn)、FORMAT(x,n)等等,对字符串进行处理。
MySQL 数字函数:常见的有COUNT(expression)、AVG(expression)、AVG(expression),对数值变量进行数学运算。
MySQL 日期函数:CURDATE(),DATE()等等,对日期类型的值进行处理,或者获取某个时间的日期。
MySQL 高级函数:CAST(x AS type),BINARY(s)等等,一些高级用法。
关于SQL编码规范,参考https://zhuanlan.zhihu.com/p/27466166
建立如下2张表,并且插入数据:
CREATE TABLE email( ID INT NOT NULL PRIMARY KEY, Email VARCHAR(255) )
```INSERT INTO email VALUES(‘1‘,‘a@b.com‘);
INSERT INTO email VALUES(‘2‘,‘c@d.com‘);
INSERT INTO email VALUES(‘3‘,‘a@b.com‘);```
CREATE TABLE World ( name VARCHAR(50) NOT NULL, continent VARCHAR(50) NOT NULL, area INT NOT NULL, population INT NOT NULL, gdp INT NOT NULL );
```INSERT INTO World
VALUES(‘Afghanistan‘,‘Asia‘,652230,25500100,20343000);
INSERT INTO World
VALUES(‘Albania‘,‘Europe‘,28748,2831741,12960000);
INSERT INTO World
VALUES(‘Algeria‘,‘Africa‘,2381741,37100000,188681000);
INSERT INTO World
VALUES(‘Andorra‘,‘Europe‘,468,78115,3712000);
INSERT INTO World
VALUES(‘Angola‘,‘Africa‘,1246700,20609294,100990000);```
Email,COUNT(Email)
FROM
email
GROUP BY
Email
HAVING
COUNT(Email) > 1;
SELECT * FROM World WHERE area >3000000 OR population > 25000000 AND gdp > 20000000;
原文:https://www.cnblogs.com/Miles-mjy/p/10636523.html