- A table for example:
1
2
3
4
5
6
7
8
9+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SELECT
SELECT FROM
- 一列中的全部值:
SELECT name FROM websites
- 一列中的uniqe值:
SELECT DISTINCT country FROM websites
SELECT * FROM
- 有条件的筛选某些行:
SELECT * FROM website WHERE country = 'CN'
SELECT * FROM website WHERE id = 1
- 多个筛选条件:
SELECT * FROM website WHERE country = "CN" AND alexa > 50
SELECT * FTOM website WHERE country = "CN" OR country = "USA"
SELECT * FROM website WHERE alexa > 50 AND (country = "CN" OR country = "USA")
ORDER BY 排序
- 给整个表格内容排序:
SELECT * FROM website ORDER BY alexa
SELECT * FROM website ORDER BY alexa DESC
- 给整个表格按多列中的内容排序:
SELECT * FROM website ORDER BY alexa, country
INSERT INTO
- 给列表中的 某几列 对应添加值
INSERT INTO website (name, url, alexa, country) VALUES ('Netflex', "www.netflex.com", "4", "USA")
UPDATE
- 先给想更新的field赋值,随后选中要改变的行
UPDATE website SET alexa = "5000", country = "USA" WHERE name = '菜鸟教程'
DELETE
- 和 SELECT类似
DELETE * FROM website WHERE name = "百度"
JOIN
1 | +-----+---------+-------+------------+ |
INNER JOIN
只要两张表在 ON语句中有能对应起来的数据,就把那一行 SELECT中的那几项返回回来
1
2
3
4SELECT websites.id, websites.name, access_log.count, access_log.date
FROM websites
INNER JOIN access_log
ON website.if = access_log.site_id;返回所有网站的浏览记录
1
2
3
4SELECT websites.name, access_log.count, access_log.data
FROM websites
INNER JOIN access_log
ON websites.id = access_log.site_id
LEFT JOIN
- 即使 右侧表中没有匹配 也返回左侧表中全部的行
- 回所有网站及他们的访问量(如果有的话)
1
2
3
4SELECT websites.name, access_log.count, access_log.date
FROM websites
LEFT JOIN access_log
ON websites.id = access_log.site_id
RIGHT JOIN
- 即使 左侧表中没有匹配 也返回右侧标表全部的行
- 返回全部网站的访问记录
1
2
3
4SELECT websites.name, access_log.count, access_log.date
FROM websites
RIGHT JOIN access_log
ON website.id = access_log.site_id
FULL JOIN
- 只要其中一个表 存在匹配 返回那一行