创建
CREATE TABLE student0(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;
NAME VARCHAR(20),
age TINYINT UNSIGNED DEFAULT 18,
height DECIMAL(5,2),
gender ENUM("男","女","中"),DEFAULT "中",
cls_id INT
);
crud
ALTER TABLE mytable ADD birthday DATE;
DESC mytable;
ALTER TABLE mytable MODIFY birthday DATE DEFAULT "1990-01-01";
ALTER TABLE mytable CHANGE birthday birth DATE DEFAULT "1990-02-02";
ALTER TABLE mytable DROP hight;
INSERT INTO mytable VALUES(6,"贝贝",1.80,"M","2000-01-01");
INSERT INTO mytable (NAME,height) VALUES("小白",1.60);
INSERT INTO mytable (NAME,gender) VALUES("小a",1),("小b",1);
UPDATE mytable SET gender="F" WHERE NAME="小白";
UPDATE mytable SET height="1.60",birth="1998-11-28" WHERE id=7;
DELETE FROM mytable WHERE id=8;
ALTER TABLE mytable ADD `is_del` INT DEFAULT 0;
UPDATE mytable SET `is_del` =1 WHERE id=10;
SELECT * FROM mytable;
SELECT NAME,gender FROM mytable;
SELECT (NAME) FROM mytable;
SELECT NAME AS "姓名", gender AS "性别" FROM mytable;
SELECT s.`gender` FROM mytable AS s;
SELECT DISTINCT NAME FROM mytable;
SELECT DISTINCT NAME,gender FROM mytable;
SELECT *FROM mytable WHERE id>3;
SELECT *FROM mytable WHERE NAME="小白";
ALTER TABLE mytable ADD age INT;
SELECT *FROM mytable WHERE age >=30 OR age<=20;
SELECT *FROM mytable WHERE id>3 AND gender=2;
SELECT *FROM mytable WHERE NOT (age=20 AND gender=2);
SELECT *FROM mytable WHERE NAME LIKE "小%";
SELECT *FROM mytable WHERE NAME LIKE "%白%";
SELECT *FROM mytable WHERE NAME LIKE "__";
SELECT * FROM mytable WHERE NAME LIKE "__%";
SELECT * FROM mytable WHERE id IN (1,4,6);
SELECT * FROM mytable WHERE age NOT IN (20,25);
SELECT * FROM mytable WHERE id BETWEEN 3 AND 6;
SELECT * FROM mytable WHERE (id BETWEEN 3 AND 6) AND gender="M";
SELECT * FROM mytable WHERE (id NOT BETWEEN 6 AND 9);
SELECT * FROM mytable WHERE birth IS NOT NULL;
SELECT COUNT(*) FROM mytable; 求 总人数
SELECT COUNT(*) FROM mytable WHERE gender="F";
SELECT MAX(age) FROM mytable;
SELECT NAME,MAX(age) AS "最大的年龄" FROM mytable;
SELECT MIN(age) FROM mytable WHERE is_del=0;
SELECT SUM(age) FROM mytable WHERE gender="f";
SELECT AVG(age) FROM mytable WHERE is_del=0 AND gender="m";
SELECT ROUND(AVG(age),2) FROM mytable WHERE is_del=0 AND gender="f";
ALTER TABLE mytable CHANGE id sid INT NOT NULL AUTO_INCREMENT;
DESC mytable;
查询练习
SELECT COUNT(*) FROM mytable GROUP BY gender;
SELECT gender "性别",COUNT(*) FROM mytable GROUP BY gender;
SELECT gender "性别",COUNT(*),GROUP_CONCAT(NAME) FROM mytable GROUP BY gender;
SELECT gender ,COUNT(*) FROM mytable GROUP BY gender WITH ROLLUP;
SELECT gender AS "性别",GROUP_CONCAT(NAME,age) FROM mytable GROUP BY gender;
SELECT gender AS "性别",GROUP_CONCAT(NAME,"-",age) FROM mytable GROUP BY gender;
SELECT gender,COUNT(*) FROM mytable GROUP BY gender WHERE COUNT(*)>2;
SELECT gender,COUNT(*) FROM mytable GROUP BY gender HAVING COUNT(*)>2;
SELECT gender ,AVG(age),GROUP_CONCAT(NAME) FROM mytable GROUP BY gender HAVING AVG(age)>18;
SELECT * FROM mytable WHERE (age BETWEEN 18 AND 30) AND gender="f" ORDER BY age;
SELECT * FROM mytable WHERE (age BETWEEN 18 AND 30) AND gender="f" ORDER BY age ASC;
SELECT * FROM mytable WHERE (age BETWEEN 18 AND 30) AND gender="f" ORDER BY age DESC;
SELECT *FROM mytable WHERE (age BETWEEN 18 AND 30) AND gender="f" ORDER BY sid DESC;
ALTER TABLE mytable ADD weight FLOAT;
DESC mytable;
SELECT gender,NAME,weight,age FROM mytable WHERE (age BETWEEN 18 AND 30) AND gender="f" GROUP BY age DESC,weight ASC;
SELECT * FROM mytable;
SELECT *FROM mytable LIMIT 2;
SELECT *FROM mytable LIMIT 6;
SELECT *FROM mytable LIMIT 3,3;
SELECT * FROM mytable LIMIT 0,3;
SELECT *FROM mytable LIMIT 3,3;
SELECT *FROM mytable LIMIT 6,3;
SELECT *FROM mytable INNER JOIN student;
SELECT *FROM mytable s INNER JOIN student c ON s.sid=c.id;
SELECT s.name,c.name FROM mytable s INNER JOIN student c ON s.`sid`=c.`id`;
SELECT s.*,c.name FROM mytable s INNER JOIN student c ON s.`sid`=c.`id`;
SELECT *FROM mytable s INNER JOIN student c ON s.`sid`=c.`id` ORDER BY s.`sid` DESC;
SELECT *FROM mytable s INNER JOIN student c ON s.`sid`=c.`id` ORDER BY s.`sid` ASC,s.`sid` ASC;
SELECT * FROM mytable s LEFT JOIN student c ON s.`sid`=c.`id`;
SELECT * FROM mytable WHERE height=1.82;
SELECT MAX(height) FROM mytable WHERE gender="f";
SELECT NAME "名字",MAX(height) FROM mytable WHERE gender="f";
SELECT NAME,height FROM mytable s WHERE s.`height`= (SELECT MAX(height) FROM mytable WHERE gender="f");
SELECT *FROM mytable WHERE height >(SELECT AVG(height) FROM mytable);
SELECT * FROM mytable WHERE gender="m" AND (SELECT MAX(age) FROM mytable);
SELECT * FROM mytable WHERE gender="m" AND age=(SELECT MAX(age) FROM mytable WHERE gender="m");
转载请注明原文地址:https://blackberry.8miu.com/read-35862.html