0

所以我有下表,do_stock_movement,看起来像这样:

stock_movement_id sm_number sm_source_id sm_destination_id
15164b86a7533d 145 1516478840ee29 151644d8bd63f2
15166b89d1a9fc 194 15165c481bd9d0 151659e632cd48

sm_source_idsm_destination_id两个参考产品点存储在do_product_points.

我正在使用以下 SQL 查询:

选择 * 从 do_stock_movement
INNER JOIN do_product_points ON product_points_id = sm_source_id
WHERE sm_number = '145'

do_product_points中,有一个名为 的字段pp_name。我需要对应pp_namesm_source_idsm_destination_id

但是,上面的查询只会返回pp_namefor sm_source_id,或者sm_destination_id如果您将连接字段更改为 for 。

什么 SQL 查询将返回对应pp_namesm_source_idsm_destination_id

我希望这很清楚。如果不是,请提出问题!

4

3 回答 3

1

JOIN此表do_product_points再一次用于sm_destination_id

SELECT 
  s.pp_name AS SourcePoint,
  d.pp_name AS DestinationPoint,
  ...
FROM do_stock_movement AS m
INNER JOIN do_product_points s ON s.product_points_id = m.sm_source_id
INNER JOIN do_product_points d ON d.product_points_id = m.sm_destination_id
WHERE m.sm_number = '145'
于 2013-04-11T13:55:08.053 回答
1

您需要加入两次并使用别名:

SELECT *, Src.pp_name, Dst.pp_name FROM do_stock_movement
  INNER JOIN do_product_points as Src 
      ON Src.product_points_id = sm_source_id
  INNER JOIN do_product_points as Dst 
      ON Dst.product_points_id = sm_destination_id
于 2013-04-11T13:56:03.203 回答
1

您需要加入 product_points 表两次,一次使用 source_id,一次使用 destination_id:

SELECT * FROM do_stock_movement move
INNER JOIN do_product_points source ON source.product_points_id = move.sm_source_id
INNER JOIN do_product_points dest ON dest.product_points_id = move.sm_destination_id
WHERE sm_number = '145'
于 2013-04-11T13:57:16.337 回答