如何让我的三个表共享相同的“part_no”字段作为主键?我需要使用三个不同的表访问“part_no”。怎么样?他们必须是同一个名字吗?
TABLE NAME: cartons_current
+------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+------------+--------------+--------+--------+-------------------+------------+
| part_no | varchar(20) | No | Prim | | |
| qty | int(8) | No | | | |
| qty_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
TABLE NAME: cartons_add
+------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+------------+--------------+--------+--------+-------------------+------------+
| part_no | varchar(20) | No | Prim | | |
| add_qty | int(8) | No | | | |
| add_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
TABLE NAME: cartons_pull
+------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+------------+--------------+--------+--------+-------------------+------------+
| part_no | varchar(20) | No | Prim | | |
| pull_qty | int(8) | No | | | |
| pull_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
答案 0 :(得分:0)
如果要选择共享主键的记录,请使用以下选择。
SELECT cc.part_no, cc.qty, cc.qty_time
, ca.add_qty as qty_added, ca.add_time as time_added
, cp.pull_qty as qty_pulled, cp.pull_time as time_pulled
FROM cartons_current cc
INNER JOIN cartons_add ca ON (ca.part_no = cc.part_no)
INNER JOIN cartons_pull cp ON (cp.part_no = cc.part_no)
WHERE cc.part_no = '124'