开发者

How do I make three tables share the same field as primary key in mySQL?

How do make my three tables share the same "part_no" field as primary key? I need to access "part_no" with three different tables. How? Do they have to be the same name?

 TABLE NAME:  cartons_current
 +------------+--------------+--------+--------+-------------------+------------+
 | Column     |  Type        |  Null  |  Key   |  Default          |  Extra     |
 +------------+--------------+--------+--------+-------------------+------------+
 | part_no    | varchar(20)  | 开发者_JAVA技巧 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 |            |
 +------------+--------------+--------+--------+-------------------+------------+


If you want to select the records that share a primary key use the following select.

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜