sql复习
-
创建数据库
CREATE DATABASEhelloworld
DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
2.create table student(
PersonID int default 0,
LastName varchar(255) not null
)
3.select
3.1 distinct去重
SELECT DISTINCT country FROM Websites;
3.2 top百分比选取
SELECT TOP 50 PERCENT * FROM Websites;3.3 into复制原表到新表
SELECT *INTO newtable [IN externaldb] FROM table1;
3.3 INSERT INTO SELECT
INSERT INTO table2 SELECT * FROM table1; -
limit
SELECT * FROM Persons LIMIT 5; -
in WHERE 子句中规定多个值。
SELECT * FROM Websites WHERE name IN ('Google','菜鸟教程');
6.like
SELECT * FROM Websites WHERE name LIKE 'G%';查找G开头
SELECT * FROM Websites WHERE name LIKE '%oo%';
7.inner join 两个表合并记录 on 条件
SELECT Websites.id, Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id; -
LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL
-
IGHT JOIN 关键字从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
-
union 合并结果 SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;