两张表的正确左连接怎么写?

How to write correct left Join of two tables?

我想连接两个 table,第一个 table 主键数据类型是数字,第二个 table 主键数据类型是 VARCHAR2(30 BYTE)。如何同时加入 tables.

我试过这段代码,但第二 tables 所有值都是空的。这是为什么?

SELECT a.act_phone_no,a.act_actdevice,a.bi_account_id, a.packag_start_date, c.identification_number, 
FROM ACTIVATIONS_POP a
left JOIN customer c
on TO_CHAR(a.act_phone_no) = c.msisdn_voice

第一个table

act_phone_no   bi_account_id
23434             45345
34245             43556    

第二个table

msisdn_voice    identification_number
23434              321113
34245              6547657

看来你并没有告诉我们所有的事情。查询works,如果写对了,在这样的样本数据上:

SQL> with
  2  -- Sample data
  3  activations_pop (act_phone_no, bi_account_id) as
  4    (select 23434, 45345 from dual union all
  5     select 34245, 43556 from dual
  6    ),
  7  customer (msisdn_voice, identification_number) as
  8    (select '23434', 321113  from dual union all
  9     select '34245', 6547657 from dual
 10    )
 11  -- query works OK
 12  select a.act_phone_no,
 13         a.bi_account_id,
 14         c.identification_number
 15  from activations_pop a join customer c on to_char(a.act_phone_no) = c.msisdn_voice;

ACT_PHONE_NO BI_ACCOUNT_ID IDENTIFICATION_NUMBER
------------ ------------- ---------------------
       23434         45345                321113
       34245         43556               6547657

SQL>

有什么问题吗?谁知道。如果你得到 一些 结果,但 CUSTOMER table 的列是空的(NULL?),那么它们真的可能是 NULL ,或者您没有设法在这些列上连接行(left/right 用空格填充?)。是否加入

on to_char(a.act_phone_no) = trim(c.msisdn_voice)

on a.act_phone_no = to_number(c.msisdn_voice)

帮助?

考虑发布适当的测试用例(CREATE TABLEINSERT INTO 语句)。

您正在使用 Oracle 吗?

请查看下面的演示

SELECT a.act_phone_no, a.bi_account_id, c.identification_number
FROM ACTIVATIONS_POP a
left JOIN customer c
on TO_CHAR(a.act_phone_no) = c.msisdn_voice;

SQLFiddle