MySQL-2
作者:互联网
MySQL 引号
MySQL里引用字符串常量时需要用一堆英文单引号 ' 或英文双引号 " 讲字符串常量括起来
数据库查询
SELECT * FROM user WHERE DATABASE()="practice" LIMIT 1 OFFSET 2;
SELECT * FROM user WHERE DATABASE()="practice" LIMIT 2, 1;
LIMIT
LIMIT X, Y
中X表示跳过X个数据,读取Y个数据
LIMIT Y
表示选取Y个数据
OFFSET
OFFSET X
表示跳过X个数据
WHERE 字句
SELECT * FROM user WHERE username="hello" AND password <> "world";
UPDATE 更新
UPDATE user SET username="hello", password="world!" WHERE DATABASE()="practice" AND username="hello" AND password="world";
DELETE 语句
DELETE FROM user WHERE username="" AND password="";
DELETE、TRUNCATE和DROP的区别
DROP > TRUNCATE > DELETE
DELETE
- delete是DML,执行delete操作时,每次从表中删除一行,并且同时将该行的删除操作记录在redo和undo表空间中以便进行回滚(rollback)和重做操作,但要注意表空间要足够大,需要手动提交才能生效
- delete可根据条件删除表中满足条件的数据,如果不指定where字句,那么删除表中所有记录
- delete语句不影响表所占用的extent,高水线(high watermark)保持原位置不变
TRUNCATE
- TRUNCATE是DDL,会隐式提交,所以不会回滚,不会触发触发器
- 会删除表中所有记录,并且重新设置高水线和所有的索引
- 对于外键约束引用的表,不能用truncate table,而应使用不带WHERE字句的DELETE语句
- 不能用于参与了索引视图的表
DROP
- DROP是DDL,会隐式提交,所以,不能回滚,不会触发触发器。
- drop语句删除表结构及所有数据,并将表所占用的空间全部释放。
- drop语句将删除表的结构所依赖的约束,触发器,索引,依赖于该表的存储过程/函数将保留,但是变为invalid状态
LIKE 语句
SELECT * FROM user WHERE password LIKE "%2%";
UNION 语句
SELECT country FROM apps WHERE id=1 UNION DISTINCT SELECT country FROM websites WHERE id=1;
MySQL 排序
SELECT * FROM user ORDER BY password ASC;
SELECT * FROM user ORDER BY password DESC;
MySQL 分组
SELECT name, COUNT(name) from employee_tbl GROUP BY name, signin;
Coalesce(a, b, c)
SELECT COALESCE(name, "总数") as "姓名", COUNT(signin) FROM employee_tbl GROUP BY name WITH ROLLUP;
MySQL 连接的使用
INNER JOIN
内连接,或等值连接。
获取两个表中字段匹配关系的记录。
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl AS a INNER JOIN tcount_tbl AS b ON a.runoob_author = b.runoob_author;
LEFT JOIN
左连接
获取左表所有记录,即使右表没有对应匹配的记录。
SELECT a.runoob_id, a.runoob_title, b.runoob_count, b.runoob_author FROM RUNOOB_TBL AS a LEFT JOIN tcount_tbl AS b ON a.runoob_author = b.runoob_author;
RIGHT JOIN
右连接
与左连接相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
SELECT a.runoob_id, a.runoob_title, b.runoob_author, b.runoob_count FROM runoob_tbl AS a RIGHT JOIN tcount_tbl AS b ON a.runoob_author = b.runoob_author;
标签:runoob,author,user,MySQL,WHERE,SELECT,tbl 来源: https://www.cnblogs.com/amazingman113/p/16086249.html