mysql-----sql语句(一)
-- 字符集: utf8mb4 -- UTF-8 Unicode -- 排序规则: utf8mb4_general_ci -- 创库 create database test2 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; -- 创表 CREATE table tutorials_tbl( tutorial_id INT NOT NULL AUTO_INCREMENT, tutorial_title VARCHAR(100) NOT NULL, tutorial_author VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( tutorial_id ) ); -- 删表 drop table tutorials_tbl; -- 插入数据 INSERT INTO tutorials_tbl (tutorial_id,tutorial_title,tutorial_author,submission_date) VALUES (1001,"LEARN MYSQL","ABDUL S",NOW()); -- 删除表里数据 DELETE FROM table_name WHERE table_name.some_colume="some_value"; DELETE from tutorials_tbl where tutorial_id="1002";
-- 更新 UPDATE table_name SET test_column1="test22_column1" where table_name.some_colume="some_value"; UPDATE tutorials_tbl set tutorials_tbl.tutorial_author = "eeewww" where tutorials_tbl.tutorial_id=1004 AND tutorials_tbl.tutorial_title="python"; -- MySQL 取第1条数据 LIMIT SELECT * from ysl.tutorials_tbl LIMIT 1; -- LIKE 通配符(%) SELECT * from tutorials_tbl where tutorials_tbl.tutorial_title LIKE "%MYSQL%"; -- IN 操作符允许在WHERE 子句中规定多个值。 SELECT * FROM TABLE_NAME WHERE TABLE_NAME.colume1 IN (value1, value2); SELECT * from tutorials_tbl where tutorials_tbl.tutorial_title IN ("LEARN MYSQL","LEARN MYSQL1") -- BETWEEN 某个值什么之间 SELECT * from tutorials_tbl where tutorials_tbl.tutorial_id BETWEEN 1001 and 1003; -- sql 别名 1 列别名 2 表别名 SELECT colume_id as id from table_name; SELECT * from table_name as t where t.colume_id="1001"
INNER JOIN
--SQL 连接(JOIN) SELECT * from access_log; +-----+---------+-------+---------------------+ | aid | site_id | count | date | +-----+---------+-------+---------------------+ | 1 | 1 | 45 | 2018-10-10 23:20:05 | | 2 | 3 | 100 | 2018-10-10 23:20:05 | | 3 | 1 | 230 | 2018-10-10 23:20:05 | | 4 | 2 | 10 | 2018-10-10 23:20:05 | | 5 | 5 | 205 | 2018-10-10 23:20:05 | | 6 | 4 | 13 | 2018-10-10 23:20:05 | | 7 | 3 | 220 | 2018-10-10 23:20:05 | | 8 | 5 | 545 | 2018-10-10 23:20:05 | | 9 | 3 | 201 | 2018-10-10 23:20:05 | +-----+---------+-------+---------------------+ SELECT * from Websites; +----+---------------+----------------------------+-------+---------+ | id | name | url | alexa | country | +----+---------------+----------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 菜鸟教程 | https://www.runoob.com/ | 4689 | CN | | 4 | 微博 | https://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | stackoverflow | https://stackoverflow.com/ | 0 | IND | +----+---------------+----------------------------+-------+---------+ SELECT Websites.id, Websites.name, access_log.count,access_log.date from Websites INNER JOIN access_log on Websites.id=access_log.site_id; +----+--------------+-------+---------------------+ | id | name | count | date | +----+--------------+-------+---------------------+ | 1 | Google | 45 | 2018-10-10 23:20:05 | | 1 | Google | 230 | 2018-10-10 23:20:05 | | 2 | 淘宝 | 10 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 100 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 220 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 201 | 2018-10-10 23:20:05 | | 4 | 微博 | 13 | 2018-10-10 23:20:05 | | 5 | Facebook | 205 | 2018-10-10 23:20:05 | | 5 | Facebook | 545 | 2018-10-10 23:20:05 | +----+--------------+-------+---------------------+ -- INNER JOIN:如果表中有至少一个匹配,则返回行 -- LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行 -- RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行 -- FULL JOIN:只要其中一个表中存在匹配,则返回行 -- 首先,连接的结果可以在逻辑上看作是由SELECT语句指定的列组成的新表。 -- 左连接与右连接的左右指的是以两张表中的哪一张为基准,它们都是外连接。 -- 外连接就好像是为非基准表添加了一行全为空值的万能行,用来与基准表中找不到匹配的行进行匹配。假设两个没有空值的表进行左连接,左表是基准表,左表的所有行都出现在结果中,右表则可能因为无法与基准表匹配而出现是空值的字段。
-- SQL INNER JOIN 语法 SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; 或: SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name; -- INNER JOIN 与 JOIN 是相同的。
LEFT JOIN
LEFT JOIN SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name; 或: SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name; -- 注释:在某些数据库中,LEFT JOIN 称为 LEFT OUTER JOIN
SELECT Websites.id, Websites.name, access_log.count,access_log.date from Websites LEFT JOIN access_log on Websites.id=access_log.site_id; +----+---------------+-------+---------------------+ | id | name | count | date | +----+---------------+-------+---------------------+ | 1 | Google | 45 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 100 | 2018-10-10 23:20:05 | | 1 | Google | 230 | 2018-10-10 23:20:05 | | 2 | 淘宝 | 10 | 2018-10-10 23:20:05 | | 5 | Facebook | 205 | 2018-10-10 23:20:05 | | 4 | 微博 | 13 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 220 | 2018-10-10 23:20:05 | | 5 | Facebook | 545 | 2018-10-10 23:20:05 | | 3 | 菜鸟教程 | 201 | 2018-10-10 23:20:05 | | 7 | stackoverflow | NULL | NULL | +----+---------------+-------+---------------------+
LEFT JOIN 关键字从左表(Websites)返回所有的行,即使右表(access_log)中没有匹配。
RIGHT JOIN
--RIGHT JOIN SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name; SELECT Websites.name, access_log.count, access_log.date FROM access_log RIGHT JOIN Websites ON access_log.site_id=Websites.id ORDER BY access_log.count DESC; +---------------+-------+---------------------+ | name | count | date | +---------------+-------+---------------------+ | Facebook | 545 | 2018-10-10 23:20:05 | | Google | 230 | 2018-10-10 23:20:05 | | 菜鸟教程 | 220 | 2018-10-10 23:20:05 | | Facebook | 205 | 2018-10-10 23:20:05 | | 菜鸟教程 | 201 | 2018-10-10 23:20:05 | | 菜鸟教程 | 100 | 2018-10-10 23:20:05 | | Google | 45 | 2018-10-10 23:20:05 | | 微博 | 13 | 2018-10-10 23:20:05 | | 淘宝 | 10 | 2018-10-10 23:20:05 | | stackoverflow | NULL | NULL | +---------------+-------+---------------------+
-- UNION 操作符合并两个或多个 SELECT 语句的结果。 2个表相同中相同的列名 -- UNION 操作符用于合并两个或多个 SELECT 语句的结果集。 -- 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。
-- ALL: 可选,返回所有结果集,包含重复数据。
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2; -- UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。 SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;
UNION
SELECT * FROM Websites; +----+---------------+----------------------------+-------+---------+ | id | name | url | alexa | country | +----+---------------+----------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 菜鸟教程 | https://www.runoob.com/ | 4689 | CN | | 4 | 微博 | https://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | stackoverflow | https://stackoverflow.com/ | 0 | IND | +----+---------------+----------------------------+-------+---------+ SELECT * FROM apps; +----+------------+--------------------------+---------+ | id | app_name | url | country | +----+------------+--------------------------+---------+ | 1 | QQ APP | http://im.qq.com/ | CN | | 2 | 微博 APP | http://weibo.com/ | CN | | 3 | 淘宝 APP | https://www.taobao.com/ | CN | +----+------------+--------------------------+---------+ -- 去重 SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country; +---------+ | country | +---------+ | CN | | IND | | USA | +---------+ -- 不去重复 SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country; +---------+ | country | +---------+ | CN | | CN | | CN | | CN | | CN | | CN | | IND | | USA | | USA | +---------+ SELECT country, name FROM Websites WHERE country='CN' UNION ALL SELECT country, app_name FROM apps WHERE country='CN' ORDER BY country; +---------+--------------+ | country | name | +---------+--------------+ | CN | QQ APP | | CN | 微博 APP | | CN | 淘宝 APP | | CN | 淘宝 | | CN | 菜鸟教程 | | CN | 微博 | +---------+--------------+
参考
http://www.runoob.com