MySQL里引用字符串常量时需要用一堆英文单引号 ' 或英文双引号 " 讲字符串常量括起来
SELECT * FROM user WHERE DATABASE()="practice" LIMIT 1 OFFSET 2; SELECT * FROM user WHERE DATABASE()="practice" LIMIT 2, 1;
LIMIT X, Y
中X表示跳过X个数据,读取Y个数据
LIMIT Y
表示选取Y个数据
OFFSET X
表示跳过X个数据
SELECT * FROM user WHERE username="hello" AND password <> "world";
UPDATE user SET username="hello", password="world!" WHERE DATABASE()="practice" AND username="hello" AND password="world";
DELETE FROM user WHERE username="" AND password="";
DROP > TRUNCATE > DELETE
SELECT * FROM user WHERE password LIKE "%2%";
SELECT country FROM apps WHERE id=1 UNION DISTINCT SELECT country FROM websites WHERE id=1;
SELECT * FROM user ORDER BY password ASC; SELECT * FROM user ORDER BY password DESC;
SELECT name, COUNT(name) from employee_tbl GROUP BY name, signin;
SELECT COALESCE(name, "总数") as "姓名", COUNT(signin) FROM employee_tbl GROUP BY name WITH ROLLUP;
内连接,或等值连接。
获取两个表中字段匹配关系的记录。
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;
左连接
获取左表所有记录,即使右表没有对应匹配的记录。
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;
右连接
与左连接相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
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;