mysql查询

+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy    | Harold | cat      |   f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy     | Harold | dog     |   f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser  | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy    | Gwen  | bird    |   f    | 1998-09-11 | NULL       |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
| Slim      | Benny  | snake  | m    | 1996-04-29 | NULL       |
| Puffball  | Diane  | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+

+----------+--------+----------+------+------------+------------+----+
| name    | owner| species  | sex   | birth        | death      | id  |
+----------+--------+----------+------+------------+------------+----+
| Fluffy    | Harold |   cat      | f     | 1993-01-04 | NULL     |  1 |
| Claws    | Gwen  |  cat      | m    | 1994-03-17 | NULL     |  2 |
| Buffy     | Harold | dog      | f      | 1989-05-13 | NULL     |  3 |
| Fang     | Benny  | dog      | m    | 1990-08-27 | NULL     |  4 |
| Bowser  | Diane  | dog      | m    | 1979-08-31 | 1995-07-29 |  5 |
| Chirpy    | Gwen  | bird     | f      | 1998-09-11 | NULL      |  6 |
| Whistler | Gwen  | bird     | NULL | 1997-12-09 | NULL     |  7 |
| Slim      | Benny  | snake  | m     | 1996-04-29 | NULL      |  8 |
| Puffball  | Diane  | hamster | f     | 1999-03-30 | NULL      |  9 |
+----------+--------+----------+------+------------+------------+----+

(1) 在宠物之中,用 pet 联结自身来进行相似种类的雄雌配对:
SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species FROM pet AS p1, pet AS p2 WHERE p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';

(2) 要找出每只宠物的年龄:

SELECT pet.name,(YEAR(date)-YEAR(birth)) AS age,remark FROM pet, event WHERE pet.name =
event.name AND event.type = 'litter';

+---------+--------+-------+
| article  | dealer | price |
+---------+--------+-------+
|    0001 | A       |  3.45 |
|    0001 | B       |  3.99 |
|    0002 | A       | 10.99 |
|    0003 | B       |  1.45 |
|    0003 | C       |  1.69 |
|    0003 | D       |  1.25 |
|    0004 | D       | 19.95 |
+---------+--------+-------+

(3)找出最贵物品的编号、销售商和价格。
SELECT article, dealer, price FROM shop WHERE price=(SELECT MAX(price) FROM shop);

(4)每项物品的的最高价格是多少?
SELECT article, MAX(price) AS price FROM shop GROUP BY article;


你可能感兴趣的:(mysql,查询,select)