从2个表中选择相同列到视图中的1列

时间:2017-04-15 13:34:39

标签: mysql sql select join view

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

这是我的第一张表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 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+

1 个答案:

答案 0 :(得分:0)

发现它们。 我只需要添加以下查询作为列

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QSurfaceFormat format;
    format.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile);

    DrawingWidget GLWidget;
    GLWidget.setFormat(format);
    GLWidget.resize(800, 600);
    GLWidget.show();

    return app.exec();
}

所以查询就像

m_painter->setPen(QColor(Qt::green));
m_painter->setBrush(QBrush(QColor(Qt::red)));
m_painter->drawText(0, 40, QString("TEST"));

for (int i = 0; i < 6; i++) {

    int x = rand() % this->width();
    int y = rand() % this->height();
    m_painter->drawRect(QRect(QPoint(x, y), QPoint(x + 15, y + 15)));
}

结果:

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