0准备工作:
查询类型为人界的剑攻击力
mysql> SELECT atk FROM sword WHERE type = '人界'; +------+ | atk | +------+ | 5400 | | 8020 | +------+ 2 rows in set (0.00 sec) 2 rows in set (0.00 sec)虽然荆戈的攻击力不大于8020,但大于5400,满足一个就能查询到
mysql> SELECT id,name,atk FROM sword WHERE atk > -> ANY(SELECT atk FROM sword WHERE type = '人界') -> ORDER BY atk DESC; +----+--------+-------+ | id | name | atk | +----+--------+-------+ | 8 | 洛神 | 20020 | | 1 | 黑风 | 10800 | | 6 | 风跃 | 9020 | | 4 | 痕兮 | 8998 | | 3 | 荆戈 | 8020 | | 9 | 隐锋 | 8020 | +----+--------+-------+ 6 rows in set (0.00 sec)虽然荆戈的攻击力大于5400,但不大于8020,所以查不到
mysql> SELECT id,name,atk FROM sword WHERE atk > -> ALL(SELECT atk FROM sword WHERE type = '人界') -> ORDER BY atk DESC; +----+--------+-------+ | id | name | atk | +----+--------+-------+ | 8 | 洛神 | 20020 | | 1 | 黑风 | 10800 | | 6 | 风跃 | 9020 | | 4 | 痕兮 | 8998 | +----+--------+-------+ 4 rows in set (0.00 sec)##### ☆☆☆☆☆将查询的结果写入另一个数据表: - 根据type的分组,通过sword的type字段,插入到sword_type表中的type_name字段
mysql> INSERT sword_type (type_name) SELECT type FROM sword GROUP BY type; Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT*FROM sword_type; +---------+-----------+ | type_id | type_name | +---------+-----------+ | 1 | 人界 | | 2 | 仙界 | | 3 | 神界 | | 4 | 道界 | | 5 | 鬼界 | | 6 | 魔界 | +---------+-----------+ 6 rows in set (0.00 sec)使用type_id更新sword中的数据
mysql> UPDATE sword INNER JOIN sword_type -> ON type = type_name -> SET type = type_id ; Query OK, 8 rows affected (0.00 sec) Rows matched: 8 Changed: 8 Warnings: 0 mysql> SELECT*FROM sword; +----+--------+-------+------+-------+------+------+ | id | name | atk | hit | crit | attr | type | +----+--------+-------+------+-------+------+------+ | 1 | 黑风 | 10800 | 400 | 400 | 木 | 3 | | 2 | 木藜 | 5400 | 200 | 200 | 木 | 1 | | 3 | 荆戈 | 8020 | 1000 | 10 | 金 | 6 | | 4 | 痕兮 | 8998 | 800 | 999 | 水 | 4 | | 5 | 逐暮 | 100 | 1000 | 10000 | 火 | 5 | | 6 | 风跃 | 9020 | 10 | 10 | 木 | 2 | | 8 | 洛神 | 20020 | 1 | 10 | 金 | 3 | | 9 | 隐锋 | 8020 | 2000 | 10 | 土 | 1 | +----+--------+-------+------+-------+------+------+ 8 rows in set (0.00 sec)此时字段值必须为sword中的attr
mysql> CREATE TABLE sword_attr ( -> attr_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> attr VARCHAR(4) NOT NULL -> ) SELECT attr FROM sword GROUP BY attr; Query OK, 5 rows affected (0.47 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> SELECT*FROM sword_attr; +---------+------+ | attr_id | attr | +---------+------+ | 1 | 土 | | 2 | 木 | | 3 | 水 | | 4 | 火 | | 5 | 金 | +---------+------+ 5 rows in set (0.00 sec)此时可以改attr和type的类型为数字,
mysql> ALTER TABLE sword -> CHANGE type type_id SMALLINT UNSIGNED NOT NULL, -> CHANGE attr attr_id SMALLINT UNSIGNED NOT NULL; Query OK, 9 rows affected (0.05 sec) Records: 9 Duplicates: 0 Warnings: 0 mysql> SELECT*FROM sword; +----+--------+-------+------+-------+---------+---------+ | id | name | atk | hit | crit | attr_id | type_id | +----+--------+-------+------+-------+---------+---------+ | 1 | 黑风 | 10800 | 400 | 400 | 2 | 3 | | 2 | 木藜 | 5400 | 200 | 200 | 2 | 1 | | 3 | 荆戈 | 8020 | 1000 | 10 | 5 | 6 | | 4 | 痕兮 | 8998 | 800 | 999 | 3 | 4 | | 5 | 逐暮 | 100 | 1000 | 10000 | 4 | 5 | | 6 | 风跃 | 9020 | 10 | 10 | 2 | 2 | | 8 | 洛神 | 20020 | 1 | 10 | 5 | 3 | | 9 | 隐锋 | 8020 | 2000 | 10 | 1 | 1 | +----+--------+-------+------+-------+---------+---------+ 9 rows in set (0.01 sec) mysql> DESC sword; +---------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(32) | NO | | NULL | | | atk | smallint(5) unsigned | NO | | NULL | | | hit | smallint(5) unsigned | NO | | NULL | | | crit | smallint(5) unsigned | YES | | 10 | | | attr_id | smallint(5) unsigned | NO | | NULL | | | type_id | smallint(5) unsigned | NO | | NULL | | +---------+----------------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec)这样表中两列重复汉字都用数字等价替换,减少表的体积。
添加两条属性
mysql> INSERT sword_attr(attr) VALUES('光'),('暗'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT*FROM sword_attr; +---------+------+ | attr_id | attr | +---------+------+ | 1 | 土 | | 2 | 木 | | 3 | 水 | | 4 | 火 | | 5 | 金 | | 6 | 光 | | 7 | 暗 | +---------+------+ 7 rows in set (0.01 sec)插入一条数据:弑神(attr_id越界)
mysql> INSERT sword VALUES(NULL,'弑神',12000,100,100,10,6); Query OK, 1 row affected (0.00 sec) mysql> SELECT*FROM sword; +----+--------+-------+------+-------+---------+---------+ | id | name | atk | hit | crit | attr_id | type_id | +----+--------+-------+------+-------+---------+---------+ | 1 | 黑风 | 10800 | 400 | 400 | 2 | 3 | | 2 | 木藜 | 5400 | 200 | 200 | 2 | 1 | | 3 | 荆戈 | 8020 | 1000 | 10 | 5 | 6 | | 4 | 痕兮 | 8998 | 800 | 999 | 3 | 4 | | 5 | 逐暮 | 100 | 1000 | 10000 | 4 | 5 | | 6 | 风跃 | 9020 | 10 | 10 | 2 | 2 | | 8 | 洛神 | 20020 | 1 | 10 | 5 | 3 | | 9 | 隐锋 | 8020 | 2000 | 10 | 1 | 1 | | 10 | 弑神 | 12000 | 100 | 100 | 10| 6 | +----+--------+-------+------+-------+---------+---------+ 10 rows in set (0.00 sec)查询所有剑的详细信息(通过内连接实现:只呈现正确连接的记录) 由于弑神的attr_id越界,连接不正确,故无法查出
mysql> SELECT id,name,atk,hit,type_name,attr FROM sword AS s -> INNER JOIN sword_type AS t ON s.type_id = t.type_id -> INNER JOIN sword_attr AS a ON s.attr_id = a.attr_id; +----+--------+-------+------+-----------+------+ | id | name | atk | hit | type_name | attr | +----+--------+-------+------+-----------+------+ | 1 | 黑风 | 10800 | 400 | 神界 | 木 | | 2 | 木藜 | 5400 | 200 | 人界 | 木 | | 3 | 荆戈 | 8020 | 1000 | 魔界 | 金 | | 4 | 痕兮 | 8998 | 800 | 道界 | 水 | | 5 | 逐暮 | 100 | 1000 | 鬼界 | 火 | | 6 | 风跃 | 9020 | 10 | 仙界 | 木 | | 8 | 洛神 | 20020 | 1 | 神界 | 金 | | 9 | 隐锋 | 8020 | 2000 | 人界 | 土 | +----+--------+-------+------+-----------+------+ 8 rows in set (0.00 sec)查询所有剑的详细信息(左外连接:左表全部,右表无匹配则置空)
mysql> SELECT id,name,atk,hit,type_name,attr FROM sword AS s -> LEFT JOIN sword_type AS t ON s.type_id = t.type_id -> LEFT JOIN sword_attr AS a ON s.attr_id = a.attr_id; +----+--------+-------+------+-----------+------+ | id | name | atk | hit | type_name | attr | +----+--------+-------+------+-----------+------+ | 1 | 黑风 | 10800 | 400 | 神界 | 木 | | 2 | 木藜 | 5400 | 200 | 人界 | 木 | | 3 | 荆戈 | 8020 | 1000 | 魔界 | 金 | | 4 | 痕兮 | 8998 | 800 | 道界 | 水 | | 5 | 逐暮 | 100 | 1000 | 鬼界 | 火 | | 6 | 风跃 | 9020 | 10 | 仙界 | 木 | | 8 | 洛神 | 20020 | 1 | 神界 | 金 | | 9 | 隐锋 | 8020 | 2000 | 人界 | 土 | | 10 | 弑神 | 12000 | 100 | 魔界 | NULL | +----+--------+-------+------+-----------+------+ 9 rows in set (0.00 sec)查询所有剑的详细信息(左外连接:右表全部,左表无匹配则置空)
mysql> SELECT id,name,atk,hit,type_name,attr FROM sword AS s -> RIGHT JOIN sword_type AS t ON s.type_id = t.type_id -> RIGHT JOIN sword_attr AS a ON s.attr_id = a.attr_id; +------+--------+-------+------+-----------+------+ | id | name | atk | hit | type_name | attr | +------+--------+-------+------+-----------+------+ | 9 | 隐锋 | 8020 | 2000 | 人界 | 土 | | 1 | 黑风 | 10800 | 400 | 神界 | 木 | | 2 | 木藜 | 5400 | 200 | 人界 | 木 | | 6 | 风跃 | 9020 | 10 | 仙界 | 木 | | 4 | 痕兮 | 8998 | 800 | 道界 | 水 | | 5 | 逐暮 | 100 | 1000 | 鬼界 | 火 | | 3 | 荆戈 | 8020 | 1000 | 魔界 | 金 | | 8 | 洛神 | 20020 | 1 | 神界 | 金 | | NULL | NULL | NULL | NULL | NULL | 光 | | NULL | NULL | NULL | NULL | NULL | 暗 | +------+--------+-------+------+-----------+------+ 10 rows in set (0.00 sec)转载于:https://www.cnblogs.com/toly-top/p/9782017.html