如何使用不匹配的ID加入我的表格

时间:2018-01-21 19:53:42

标签: sql sql-server tsql

我有这些表格:

付款

| id | src_account | dest_account | payment_value |  
+----+-------------+--------------+---------------+
|  1 | xxxxxxxxxxx | yyyyyyyyyyyy |      200      |  

client_account

| client_id | account_id  | 
+-----------+-------------+ 
|     3     | xxxxxxxxxxx |  
|     4     | yyyyyyyyyyy |

客户端

| id | firstname |  lastname  |
+----+-----------+------------+
|  3 |   Sofia   |  Lenhark   |
|  4 |   Mark    |  Davoreski |

现在我需要select SQL查询中的以下字段:

  • 来源名字
  • 来源姓氏
  • 目的地名字
  • 目的地姓氏
  • 付款金额

我尝试了以下内容:

SELECT
srcClientDetails.firstname,
srcClientDetails.lastname,
destClientDetails.firstname,
destClientDetails.lastname,
Payments.payment_value

FROM
dbo.payment AS Payments
LEFT JOIN dbo.client_account AS srcClientAccount ON Payments.src_account = srcClientAccount.account_id
LEFT JOIN dbo.client AS srcClientDetails ON srcClientAccount.client_id = srcClientDetails.id
LEFT JOIN dbo.client_account AS destClientAccount ON Payments.dest_account = destClientAccount.account_id
LEFT JOIN dbo.client AS destClientDetails ON destClientAccount.client_id = destClientDetails.id

但这只返回付款值,没有任何其他数据。

为什么还没有返回其他所需的字段?

2 个答案:

答案 0 :(得分:2)

建议

(1)在INNER JOIN中尝试

要检查JOIN是否正确(值相等),请先使用INNER JOIN

测试查询
SELECT *
FROM dbo.payment AS Payments
INNER JOIN dbo.client_account AS srcClientAccount ON Payments.src_account = 
srcClientAccount.account_id

并检查它是否返回值,然后添加另一个表:

SELECT *
FROM dbo.payment AS Payments
INNER JOIN dbo.client_account AS srcClientAccount ON Payments.src_account = 
srcClientAccount.account_id
INNER JOIN dbo.client AS srcClientDetails ON srcClientAccount.client_id = srcClientDetails.id

依此类推......这可能会引发你的问题

(2)由于整理或额外空格而导致的帐户ID差异

我认为这会引发您的问题,可能是由于区分大小写的排序规则或长度敏感或其他空间造成的。要诊断此问题,您还可以尝试将两列转换为相同的大小写和长度,并修剪其他空格。

ON LTRIM(RTRIM(UPPER(CAST(Payments.src_account as varchar(50))))) = LTRIM(RTRIM(UPPER(CAST(srcClientAccount.account_id as varchar(50)))))

如果这解决了问题,那么最好编辑数据(添加关系或约束)而不是在Join中使用此函数,因为它会降低性能并且不使用索引。< /强>

(3)创建表之间的关系

从问题看来,列之间没有关系,创建关系以便限制值。

答案 1 :(得分:0)

您的逻辑是正确的,只有[client_account].account_id为1 'y'匹配的缩写,或Payments.dest_account需要少1 'y'

例如,您可以在此处看到我向'y'添加了1 [client_account].account_id,您的查询现在会返回正确的信息:

WITH Payments AS (
    SELECT '1' as id,
    'xxxxxxxxxxx' as src_account,
    'yyyyyyyyyyyy' as dest_account,
    '200' as payment_value),
client_account AS (
    SELECT '3' as client_id,
    'xxxxxxxxxxx' as account_id
    UNION ALL
    SELECT '4' as client_id,
    'yyyyyyyyyyyy' as 'account_id'),  --added a 'y' here
client AS (
    SELECT '3' as id,
    'Sofia' as firstname,
    'Lenhark' as lastname
    UNION ALL
    SELECT '4' as id,
    'Mark' as firstname,
    'Davoreski' as lastname)


SELECT
srcClientDetails.firstname,
srcClientDetails.lastname,
destClientDetails.firstname,
destClientDetails.lastname,
Payments.payment_value
FROM
payments AS Payments
LEFT JOIN client_account AS srcClientAccount ON Payments.src_account = srcClientAccount.account_id
LEFT JOIN client AS srcClientDetails ON srcClientAccount.client_id = srcClientDetails.id
LEFT JOIN client_account AS destClientAccount ON Payments.dest_account = destClientAccount.account_id
LEFT JOIN client AS destClientDetails ON destClientAccount.client_id = destClientDetails.id

enter image description here

Try it here