【问题标题】:SQL INNER JOIN with MULTIPLE CONDITION in CodeIgniterCodeIgniter 中具有多个条件的 SQL INNER JOIN
【发布时间】:2025-12-11 02:35:01
【问题描述】:

我有下表。

表格卡片

------------------------------------
| card_no          | approval_code |
------------------------------------
| 999999xxxxxx1234 | 111111        |
 ----------------------------------
| 888888xxxxxx5678 | 222222        |
------------------------------------
| 777777xxxxxx9012 | 333333        |
 -----------------------------------
| 666666xxxxxx3456 | 444444        |
 -----------------------------------

事务

---------------------------------------------
| trans_id | pan   | approval code  | amount |
---------------------------------------------
| A1       | 9012  | 333333         |     9.9|
----------------------------------------------
| A2       | 9012  | 333333         |    10.0|
----------------------------------------------
| B1       | 1233  | 111111         |    11.0|
----------------------------------------------
| B2       | 1234  | 111111         |    12.0|
----------------------------------------------
| C1       | 5678  | 222222         |    13.0|
----------------------------------------------
| C2       | 5678  | 444444         |    13.0|
----------------------------------------------

我的输出是显示 3 种类型的输出。 第一个输出是显示匹配的数据,第二个输出是显示卡表中的不匹配数据,第三个输出是显示交易表中的不匹配数据。以下是我的代码。交易表只存储 pan(来自 card_no 的最后 4 位)因此我需要 substr 来获取最后 4 位。

对于匹配的数据:

$query = $this->db->select (array(
'c.card_no', 'c.approval_code',
't.trans_id','t.pan','t.approval_code','amount'
),false)
-> join('transactions t','t.approval_code = c.approval_code','inner')
-> join('transactions t','t.pan = substr(c.card_no,12)','inner')
-> get('cards c');

卡表中不匹配的数据;

$query = $this->db->select (array(
'c.card_no', 'c.approval_code',
't.trans_id','t.pan','t.approval_code','amount'
),false)
-> join('transactions t','t.approval_code != c.approval_code','inner')
-> join('transactions t','t.pan != substr(c.card_no,12)','inner')
-> where ('t.approval_code' IS NULL,NULL,FALSE)
-> where ('t.pan' IS NULL,NULL,FALSE)
-> get('cards c');

交易表中不匹配的数据;

$query = $this->db->select (array(
'c.card_no', 'c.approval_code',
't.trans_id','t.pan','t.approval_code','amount'
),false)
-> join('cards c','t.approval_code = c.approval_code','inner')
-> join('cards c','t.pan != substr(c.card_no,12)','inner')
-> where ('c.approval_code' IS NULL,NULL,FALSE)
-> where ('c.card_no' IS NULL,NULL,FALSE)
-> get('cards c');

两者似乎都不起作用..嗯 我的预期输出是:

输出匹配的数据

--------------------------------------------------------
| card_no          | approval_code | trans_id | amount |
--------------------------------------------------------
| 999999xxxxxx1234 | 111111        | B2       | 12.0   |
 -------------------------------------------------------
| 888888xxxxxx5678 | 222222        | C1       | 13.0   |
--------------------------------------------------------
| 777777xxxxxx9012 | 333333        | A1       | 9.9    |
 -------------------------------------------------------
| 777777xxxxxx9012 | 333333        | A2       | 10.0   |
 -------------------------------------------------------

输出表格卡片中不匹配的数据

------------------------------------
| card_no          | approval_code |
------------------------------------
| 666666xxxxxx3456 | 444444        |
 -----------------------------------

输出表事务中不匹配的数据

---------------------------------------------
| trans_id | pan   | approval code  | amount |
---------------------------------------------
| B1       | 1233  | 111111         |    11.0|
----------------------------------------------
| C2       | 5678  | 444444         |    13.0|
----------------------------------------------

【问题讨论】:

  • 要匹配 IS NULL 你需要 left 加入,而不是 inner 对吗?
  • 我还是卡住了.. 两种都试了.. 我还是没有得到我想要的结果.. 请帮忙.. 谢谢
  • 我从交易中获得了不匹配的数据,但无法从卡中获得..

标签: mysql sql codeigniter


【解决方案1】:

要从cards 表中获取不匹配的数据,请将join 替换为right join。对于来自transactions 表的不匹配数据,在各自的查询中将join 替换为left join

【讨论】:

  • 我试过了,还是没有正确得到匹配和不匹配的数据。
最近更新 更多