数据库
首页 > 数据库> > MySQL-2

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

TRUNCATE

DROP


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