MySql 学习之路-基础
点击上方“五角钱的程序员”,选择“设为星标”
第一时间关注技术干货!
✨一起学习、成长、温情的热爱生活✨
图丨pixabay
作者:Linux超
原文链接:http://nxw.so/4B4wW
一.基础
数据库操作
Show databases;显示所有的数据库
Show tables; 显示所有的数据库表
Use databasename;切换数据库
Desc tablename; 显示表中所有的字段信息
说明:笔记中所使用的sql语句均使用表websites 和 access_log
表1:websites
+----+--------------+---------------------------+-------+---------+
| 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 |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
表2:access_log
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
1.LIKE 操作符
1 select * from websites where url like 'https%';-- url包含https的数据
2 select * from websites where name like 'G%'; -- name以G开头
3 select * from websites where url like '%h%';-- url包含h的字段数据
4 select * from websites where name like '_o%';-- name中o在第二个字符的数据
2.ORDER BY 关键字
1 select name,alexa from websites
2 order by alexa desc; -- 默认时升序 desc 表示降序
3
4 select * from websites
5 order by country, alexa; -- 多列排序时,先排country 在排alexa
3.INSERT INTO
1 insert into websites
2 values (6,'百度','http://www.baidu.com',22,'ZG');-- 可以省略列,但要所有的值都插入
3
4 insert into websites (id ,name,url,alexa,country)
5 values ('7','网易','www.wangyi.com','25','ZG');-- 可以指定插入哪一列
4.UPDATE
update websites
set url = 'http://www.wangyi.com'
where id = 7;
update websites
set alexa = 5000, country = 'USA'
where id = 3;
5.DISTINCT
SELECT DISTINCT country FROM Websites;
6.DELETE
-- 删除一行数据
SELECT FROM WEBSITES
WHERE ID = 7;
-- 删除整个表,但是表的结构仍存在
SELECT FROM WEBSITES;
7.AND, OR ,IN
1 SELECT * FROM WEBSITES
2 WHERE ID BETWEEN 1 AND 3;
3
4 SELECT * FROM WEBSITES
5 WHERE ID = 1 OR ID =3;-- 筛选id为1和3 的数据
6
7 SELECT * FROM WEBSITES
8 WHERE ID IN (1,3);
今天就到这,欢迎大家评论留言,一起学习一起进步!
评论