#插入数据
[mysql>create table if not exists exam_score( ..>id int(4) not null primary key auto_increment, ..>name char(20) not null, ..>score double(6,2)); #用多个list插入多行数据 [mysql> insert into exam_score values (1,‘Zhao‘,95.33),(2,‘Qian‘,94.33),(3,‘Sun‘,44.55),(4,‘Li‘,33.55); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 #展示整张表 mysql> select * from exam_score; +----+------+-------+ | id | name | score | +----+------+-------+ | 1 | Zhao | 95.33 | | 2 | Qian | 94.33 | | 3 | Sun | 44.55 | | 4 | Li | 33.55 | +----+------+-------+ 4 rows in set (0.00 sec) mysql> insert into exam_score values (5,‘Zhou‘,66.33),(6,‘Wu‘,99.32),(7,‘Zheng‘,33.2),(8,‘Wang‘,99.3); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 #查询按照字段id在0到2之间的行数据 mysql> select * from exam_score order by id limit 0,2; +----+------+-------+ | id | name | score | +----+------+-------+ | 1 | Zhao | 95.33 | | 2 | Qian | 94.33 | +----+------+-------+ 2 rows in set (0.00 sec) #查询整张表中的前两行 mysql> select * from exam_score limit 0,2; +----+------+-------+ | id | name | score | +----+------+-------+ | 1 | Zhao | 95.33 | | 2 | Qian | 94.33 | +----+------+-------+ 2 rows in set (0.00 sec) #按照name字段升序显示整张表 mysql> select * from exam_score order by name asc; +----+-------+-------+ | id | name | score | +----+-------+-------+ | 4 | Li | 33.55 | | 2 | Qian | 94.33 | | 3 | Sun | 44.55 | | 8 | Wang | 99.30 | | 6 | Wu | 99.32 | | 1 | Zhao | 95.33 | | 7 | Zheng | 33.20 | | 5 | Zhou | 66.33 | +----+-------+-------+ 8 rows in set (0.01 sec) #按照name降序显示 mysql> select * from exam_score order by name desc; +----+-------+-------+ | id | name | score | +----+-------+-------+ | 5 | Zhou | 66.33 | | 7 | Zheng | 33.20 | | 1 | Zhao | 95.33 | | 6 | Wu | 99.32 | | 8 | Wang | 99.30 | | 3 | Sun | 44.55 | | 2 | Qian | 94.33 | | 4 | Li | 33.55 | +----+-------+-------+ 8 rows in set (0.00 sec) #where条件查询 mysql> select * from exam_score where name=‘Li‘; +----+------+-------+ | id | name | score | +----+------+-------+ | 4 | Li | 33.55 | +----+------+-------+ 1 row in set (0.00 sec) mysql> select * from exam_score where id=3; +----+------+-------+ | id | name | score | +----+------+-------+ | 3 | Sun | 44.55 | +----+------+-------+ 1 row in set (0.00 sec) mysql> select * from exam_score where name=‘Zhao‘ and score<=99.0; +----+------+-------+ | id | name | score | +----+------+-------+ | 1 | Zhao | 95.33 | +----+------+-------+ 1 row in set (0.00 sec)
时间: 2024-10-24 11:08:49