从另一个表中为mysql中第一个表中的每一行选择对应的行

时间:2019-03-04 13:17:44

标签: mysql

我有一个名为 users 的表,另一个名为提现的表

如何为用户的每个提款请求选择一行,并在用户表中选择其银行详细信息。

重要提示:用户表中的用户名对于每一行都是唯一的。这对应于提现表中的用户ID

在这里, 用户表

+-----------+-----------------+-------------+
| user_name | bank_ac         | ifsc        |
+-----------+-----------------+-------------+
| H6426012  | 456456 | ifsc6544 |
+-----------+-----------------+-------------+

提取表格

+----------+--------+--------+
| userid   | amount | status |
+----------+--------+--------+
| H6426012 | 300.00 |      1 |
| H6426012 | 150.00 |      1 |
| H6426012 | 200.00 |      1 |
+----------+--------+--------+

这是我正在使用的查询:

select withdraws.userid 
     , withdraws.amount
     , users.user_name
     , users.ifsc
     , users.bank_ac 
  from withdraws 
  JOIN users 
    on withdraws.userid = users.user_name 
 where withdraws.status = 1

这是我期望的结果:

我希望得到这个结果

+----------+--------+--------++----------+--------+--------+
| userid   | amount | status |user_name   | ifsc | bank_ac |
+----------+--------+--------++----------+--------+--------+
| H6426012 | 300.00 |      1 | H6426012 | ifsc6544 | 456456 |
| H6426012 | 150.00 |      1 | H6426012 | ifsc6544 | 456456|
| H6426012 | 200.00 |      1 | H6426012 | ifsc6544 | 456456 |
+----------+--------+--------++----------+--------+--------+

但是我得到了这个结果

+----------+--------+--------++----------+--------+--------+
| userid   | amount | status |user_name   | ifsc | bank_ac |
+----------+--------+--------++----------+--------+--------+
| H6426012 | 300.00 |      1 | NULL      | NULL    | NULL |
| H6426012 | 150.00 |      1 | NULL      | NULL    | NULL|
| H6426012 | 200.00 |      1 | NULL      | NULL    | NULL |
+----------+--------+--------++----------+--------+--------+

2 个答案:

答案 0 :(得分:1)

我运行了相同的查询,但效果很好,我也创建了相同的查询,但没有任何连接,而且效果也很好... 我正在使用SQL Server Management Studio V17.9。

答案 1 :(得分:0)

首先在您的用户模式/表上将用户名更改为 user_id ,以免将来使您感到困惑。然后,如下所示修改您的查询,即可正常运行

select users.userid as user_id, 
     withdraws.amount as amount,
     withdraws.status as status
     users.ifsc as ifsc
     users.bank_ac as bank_ac
  from withdraws 
  JOIN users 
    on withdraws.userid = users.userid 
 where withdraws.status = 1