创建如下所示的courses 表 ,有: student (学生) 和 class (课程)。
编写一个 SQL 查询,列出所有超过或等于5名学生的课。
应该输出:
±--------+ | class | ±--------+ | Math | ±--------+
Note:
学生在每个课中不应被重复计算。
mysql> use yiibaidb;
mysql> CREATE TABLE IF NOT EXISTS courses (
-> student VARCHAR(50) NOT NULL,
-> class VARCHAR(50) NOT NULL
-> );
mysql> INSERT INTO courses VALUES('A','Math');
mysql> INSERT INTO courses VALUES('B','English');
mysql> INSERT INTO courses VALUES('C','Math');
mysql> INSERT INTO courses VALUES('D','Biology');
mysql> INSERT INTO courses VALUES('E','Math');
mysql> INSERT INTO courses VALUES('F','Computer');
mysql> INSERT INTO courses VALUES('G','Math');
mysql> INSERT INTO courses VALUES('H','Math');
mysql> INSERT INTO courses VALUES('I','Math');
mysql> INSERT INTO courses VALUES('A','Math');
构建courses表,含有student和class两项。
mysql> select * from courses
-> ;
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
| A | Math |
+---------+----------+
剔除表格的重复项并group之后用having筛选:
mysql> ALTER IGNORE TABLE courses ADD PRIMARY KEY (student,class);
mysql> select * from courses;
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+----------+
mysql> select class from courses group by class having count(*)>=5;
+-------+
| class |
+-------+
| Math |
+-------+
性别交换。有m=男性 和 f=女性的值 。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。
构建salary表。
mysql> create table if not exists salary (
-> id int NOT NULL PRIMARY KEY,
-> name VARCHAR(50) NOT NULL,
-> sex VARCHAR(50) NOT NULL,
-> salary INT NOT NULL
-> );
mysql> INSERT INTO salary VALUES(1,'A','m',2500);
mysql> INSERT INTO salary VALUES(2,'B','f',1500);
mysql> INSERT INTO salary VALUES(3,'C','m',5500);
mysql> INSERT INTO salary VALUES(4,'D','f',500);
mysql> select * from salary;
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
+----+------+-----+--------+
用update和replace进行替换
mysql> UPDATE `salary` SET `sex`=replace(`sex`,'f','M');
mysql> UPDATE `salary` SET `sex`=replace(`sex`,'m','f');
mysql> UPDATE `salary` SET `sex`=replace(`sex`,'M','m');
mysql> select * from salary;
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
+----+------+-----+--------+
在数据库中创建表1和表2,并各插入三行数据(自己造)
构建表格Person和Address.
mysql> create table if not exists Person (
-> PersonId int not null primary key,
-> FirstName VARCHAR(50) NOT NULL,
-> LastName VARCHAR(50) NOT NULL
-> );
mysql> INSERT INTO Person VALUES('1','ming','li');
mysql> INSERT INTO Person VALUES('2','ya','wang');
mysql> INSERT INTO Person VALUES('3','feili','sun');
mysql> select * from Person;
+----------+-----------+----------+
| PersonId | FirstName | LastName |
+----------+-----------+----------+
| 1 | ming | li |
| 2 | ya | wang |
| 3 | feili | sun |
+----------+-----------+----------+
mysql> create table if not exists Address (
-> AddressId int not null primary key,
-> PersonId int not null,
-> City VARCHAR(50),
-> State VARCHAR(50)
-> );
mysql> INSERT INTO Address VALUES(0001,2,'','guangdong');
mysql> INSERT INTO Address VALUES(0002,3,'chengdu','sichuan');
mysql> INSERT INTO Address VALUES(0003,1,'','');
mysql> select * from Address;
+-----------+----------+---------+-----------+
| AddressId | PersonId | City | State |
+-----------+----------+---------+-----------+
| 1 | 2 | | guangdong |
| 2 | 3 | chengdu | sichuan |
| 3 | 1 | | |
+-----------+----------+---------+-----------+
mysql> select P.FirstName,P.LastName as LastName, A.City,A.State as State from Person P,Address A where P.PersonId=A.PersonId;
+-----------+----------+---------+-----------+
| FirstName | LastName | City | State |
+-----------+----------+---------+-----------+
| ming | li | | |
| ya | wang | | guangdong |
| feili | sun | chengdu | sichuan |
+-----------+----------+---------+-----------+
编写一个 SQL 查询,来删除 email 表中所有重复的电子邮箱,重复的邮箱里只保留 **Id ***最小 *的那个。
mysql> select ID,Email from email group by Email having ID=min(ID);
+----+---------+
| ID | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
+----+---------+