简单的MySQL加入varchar(100)字段不起作用

Ins*_*DHD 4 mysql

所以,我正在加入mysql以过滤掉一些不良数据,我遇到了这个奇怪的问题.

  • 两个表都连接在一起payment_transaction_id.
  • 他们都有价值3463.
  • 联接结果不返回任何行.
  • 两个表都有此值.

证明记录在card_transaction_log中:

select count(*)
from card_transaction_log 
where payment_transaction_id = 3463;
>> 1
Run Code Online (Sandbox Code Playgroud)

证明记录在交易中:

select count(*)
from transaction 
where payment_transaction_id = 3463;
>> 1
Run Code Online (Sandbox Code Playgroud)

但是加入不起作用.

select count(*)
from card_transaction_log a, transaction b
where a.payment_transaction_id = b.payment_transaction_id
and a.payment_transaction_id = 3463;
>> 0
Run Code Online (Sandbox Code Playgroud)

老实说,我以前从未在mysql中见过这样的东西.我甚至和我的同事核实过,以确保我不会疯狂和/或愚蠢.

更新:

虽然这与上面相同,但此查询也不起作用:

select count(*)
from card_transaction_log a
join transaction b
on a.payment_transaction_id = b.payment_transaction_id
where a.payment_transaction_id = 3463;
>> 0
Run Code Online (Sandbox Code Playgroud)

fth*_*lla 7

什么类型payment_transaction_id?我怀疑它不是INT而是VARCHAR.如果您尝试将VARCHAR与INT进行比较,MySQL将自动将其转换为INT,但可能会发生一些奇怪的事情,例如:

'3463' = 3463
Run Code Online (Sandbox Code Playgroud)

但是也

'3463a' = 3463
Run Code Online (Sandbox Code Playgroud)

'3463a' != '3463b'
Run Code Online (Sandbox Code Playgroud)

请看这里的小提琴.您可以像这样测试您的查询:

select count(*)
from card_transaction_log 
where payment_transaction_id = '3463';
Run Code Online (Sandbox Code Playgroud)

并且我怀疑至少有一个查询将返回0.或者您可以强制您的联接使用整数值:

select count(*)
from card_transaction_log a
join transaction b
on a.payment_transaction_id+0 = b.payment_transaction_id+0
where a.payment_transaction_id = 3463;
Run Code Online (Sandbox Code Playgroud)