4

So, I'm making a join for mysql to filter out some bad data and I run into this weird issue.

  • Both tables are connected by payment_transaction_id.
  • They both have the value 3463.
  • A joined result returns no rows.
  • Both tables have this value.

Proof that the record is in card_transaction_log:

select count(*)
from card_transaction_log 
where payment_transaction_id = 3463;
>> 1

Proof that the record is in transaction:

select count(*)
from transaction 
where payment_transaction_id = 3463;
>> 1

But the join doesn't work.

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

Honestly, I've never seen anything like this before in mysql. I even checked with my colleague to make sure that I wasn't going crazy and/or dumb.

UPDATE:

While this is the same as above, this query doesn't work either:

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
4

4 回答 4

8

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

'3463' = 3463

但是也

'3463a' = 3463

'3463a' != '3463b'

在此处查看小提琴。您可以像这样测试您的查询:

select count(*)
from card_transaction_log 
where payment_transaction_id = '3463';

我怀疑您的查询中至少有一个会返回 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;
于 2013-05-13T23:25:58.733 回答
1

在我的情况下,这原来是因为数据中的隐藏字符。

我使用以下方法导入了一个包含电子邮件列表的 CSV:

LOAD DATA INFILE 'c:/temp/users.csv' 
INTO TABLE cloudusers 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 0 ROWS;

因此,每个 VARCHAR 末尾都有一个返回字符。字符串看起来相同,但不是。

当我使用重新进行导入时

LOAD DATA INFILE 'c:/temp/users.csv' 
INTO TABLE cloudusers 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 ROWS;

(注意 '\r\n' 而不是 '\n')生成的 VARCHARs 随后匹配,并且所有连接都有效。

于 2019-12-12T17:49:56.530 回答
0

我今天也遇到了同样的麻烦,这就是我发现的。即使表 A 上的 SQL 选择和表 B 上的 SQL 选择显示主键和外键的值相同,但这些值实际上是不同的......一个“\n”,当值显示在一个网格,是我所有外键值的结束字符...根本原因,我没有注意我用来通过“导入”填充表的文件是在互联网上找到的损坏的 CSV 文件。

您可以进行快速测试,只需手动更新一些预计将加入并再次测试您的查询的外键和主键值。如果它返回结果,您就找到了根本原因!

于 2014-10-13T17:39:06.333 回答
0

你能试一下吗:

select count(*)
from card_transaction_log a
join transaction b
on a.payment_transaction_id = b.payment_transaction_id
where a.payment_transaction_id = 5081483008;
于 2013-05-13T23:11:51.010 回答