sql基础

一、insert into语句
(1)插入新的一行数据
INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
(2)在指定的列中插入数据
INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');
二、修改表数据
1、update语句
(1)无条件更新
update userinfo set userpwd='111',email='[email protected]';
(2)有条件更新
update user_info set USERNAME='qqq' where tel='XXX';
三、删除表数据
1、delete语句
无条件删除
delete from use userinfo;
有条件删除
delete from userinfo where username='XXX';
四、查询语句
1、select distinct 语句
从某列中仅选取唯一不同的值
SELECT DISTINCT username from user_info limit 10;
五、top子句
SELECT TOP 50 PERCENT * FROM Persons;#从persons表中取50%的记录
六、like操作符,sql通配符
SELECT * from user_info where nick like '昔年半夏%'; #从user_info表中查询名字中带有昔年半夏的数据
SELECT * from user_info where EMAIL like '%15' ; #从user_info表中查询email中以15结尾的数据
SELECT * from user_info where nick not like'%昔年半夏%' ; #从user_info表中查询nick中不带有昔年半夏的数据
SELECT * from user_info where NICK LIKE'_年' limit 2; #从user_info表中查询nick中第一个字符后是“年”的数据
七、join查询
(1)用where联表查询
SELECT b.user_id,a.nick from user.user_info a, shop.shop b where a.id=b.user_id;
(2)Join(Inner Join)查询:INNER JOIN关键字在表中存在一个匹配时返回行
SELECT b.id,a.tel from user.user_info a INNER JOIN shop.shop b ON a.id=b.user_id limit 5;
(3)左外连接left join(left out join);left join关键字会在左表那里返回所有的行,即使在右表中没有匹配的行
SELECT a.id,a.tel,a.nick from user.user_info a left JOIN shop.shop b ON a.id=b.user_id ;
(4)右外连接Right Join(Right outer join):right join 关键字会从右表那里返回所有的行,即使在左表中没有匹配的行
SELECT a.id,a.tel,a.nick,b.SOURCE from user.user_info a RIGHT JOIN shop.shop b ON a.id=b.user_id;

你可能感兴趣的:(sql基础)