【问题标题】:Select Same Column From 2 Tables into 1 Column in View从 2 个表中选择相同的列到视图中的 1 列
【发布时间】:2017-04-15 13:34:39
【问题描述】:

我在不同的表中有两个同名的列。 我想将它们加入视图中的一列。

这是我的第一张桌子stocks

+----------+------------+------------+---------+----------------+--------+
| stock_id | stock_cost | stock_left | item_id | purchasedtl_id | trx_id |
+----------+------------+------------+---------+----------------+--------+
|        1 |       1000 |          0 |       1 |              1 |      1 |
|        2 |       1000 |          5 |       1 |              2 |      2 |
|        3 |       1000 |          1 |       1 |              3 |      4 |
+----------+------------+------------+---------+----------------+--------+

第二桌stocks_out

+-------------+----------------+--------------+---------+----------+------------+--------+
| stockout_id | stockout_price | stockout_qty | item_id | stock_id | saledtl_id | trx_id |
+-------------+----------------+--------------+---------+----------+------------+--------+
|           1 |           2000 |            1 |       1 |        1 |          1 |      3 |
+-------------+----------------+--------------+---------+----------+------------+--------+

我想加入他们成为这样的trx_id, trx_no, trx_closetime, trx_type stock_id, stock_cost, stockout_id, stock_out_cost, stockout_price, item_id

item_id 是我想在一列中加入的字段。

当前查询是:

select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price` from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;

以及目前的结果:

+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| trx_id | trx_no              | trx_closetime       | trx_type | stock_id | stock_cost | stockout_id | stockout_price |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
|      1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2        |        1 |       1000 |        NULL |           NULL |
|      2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2        |        2 |       1000 |        NULL |           NULL |
|      3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1        |     NULL |       NULL |           1 |           2000 |
|      4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2        |        3 |       1000 |        NULL |           NULL |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+

【问题讨论】:

  • 您的示例数据和查询没有任何共同之处。您应该这样表述,以便描述、数据和查询匹配。例如,您的示例结果集中没有 item_id
  • 我找到了答案。无论如何感谢@GordonLinoff!其实我忘了解释一个事务不能在 2 个表中。

标签: mysql sql select join view


【解决方案1】:

找到了,伙计们。 我只需要将以下查询添加为列

COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id`

所以查询会是这样的

select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price`, COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;

结果:

+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| trx_id | trx_no              | trx_closetime       | trx_type | stock_id | stock_cost | stockout_id | stockout_price | item_id |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
|      1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2        |        1 |       1000 |        NULL |           NULL |       1 |
|      2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2        |        2 |       1000 |        NULL |           NULL |       1 |
|      3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1        |     NULL |       NULL |           1 |           2000 |       1 |
|      4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2        |        3 |       1000 |        NULL |           NULL |       1 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多