我有两个表issue_details
和payees
,如下所示,值为:
表: issue_details
+----------+------------+----------+----------+
| check_no | issue_date | payee_id | lddap_no |
+----------+------------+----------+----------+
| 0 | 2015-01-01 | | 11101-15 |
| 1 | 2015-01-24 | | 11101-15 |
| 34561 | 2015-01-24 | 10 | |
| 34562 | 2015-01-24 | 13 | |
+----------+------------+----------+----------+
表:收款人
+----------+--------------------+
| payee_id | payee_name |
+----------+--------------------+
| 1 | John Smith |
| 10 | Chen Lou |
| 11 | Mark Dello |
| 12 | ANTRECCO |
| 13 | Shay Ridz |
+----------+--------------------+
我执行此查询以连接两个表来描述我的issue_details
表中的收款人姓名。
SELECT
issue_details.issue_date AS date,
issue_details.check_no AS check_no,
payees.payee_name AS payee
FROM
issue_details
INNER JOIN
payees ON issue_details.payee_id = payees.payee_id
结果:
+----------+------------+----------+
| check_no | issue_date | payee |
+----------+------------+----------+
| 34561 | 2015-01-24 | Chen Lou |
| 34562 | 2015-01-24 | Shay Ridz|
+----------+------------+----------+
如果我这样做:
SELECT
issue_details.issue_date AS date,
issue_details.check_no AS check_no,
CONCAT(payees.payee_name, '', lddap_no) AS payee
FROM
issue_details
INNER JOIN
payees ON issue_details.payee_id = payees.payee_id
结果仍然如上。它没有连接两列
payee_name
(从表收款人加入)和lddap_no
。
这就是我想要发生的事情:
+----------+------------+----------+
| check_no | issue_date | payee |
+----------+------------+----------+
| 0 | 2015-01-01 | 11101-15 |
| 1 | 2015-01-24 | 11101-15 |
| 34561 | 2015-01-24 | Chen Lou |
| 34562 | 2015-01-24 | Shay Ridz|
+----------+------------+----------+
请在这里帮助我查询。非常感谢。感谢
答案 0 :(得分:0)
SELECT id.issue_date AS date,
id.check_no AS check_no,
COALESCE( p.payee_name, id.lddap_no ) AS payee
FROM issue_details id
LEFT JOIN payees p ON
id.payee_id = p.payee_id
答案 1 :(得分:0)
只需将INNER JOIN
更改为LEFT JOIN
SELECT issue_details.issue_date AS date,
issue_details.check_no AS check_no,
payees.payee_name AS payee
FROM
issue_details
LEFT JOIN payees ON
issue_details.payee_id = payees.payee_id