Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 使用 CONVERT ( c.val0, SIGNED ) 语法 解决 两张表的关联字段 id 数据类型不同,关联出错问题

【TiDB Usage Environment】Production Environment / Testing / Poc
【TiDB Version】5.4
【Reproduction Path】SQL self-join query on the same table
【Encountered Problem: Phenomenon and Impact】
SQL as follows:
SELECT
a.id,
(
SELECT
IFNULL(sum(c.val2), 0)
FROM
fac_app_data_table_field_val c
WHERE
c.val0 = a.id
AND c.table_id = 1969241186793349598
) AS r15
FROM
fac_app_data_table_field_val a
LEFT JOIN fac_app_data_table_field_val b ON a.id = b.val0
AND b.table_id = 1969243196838052179
AND b.school_id = '22'
AND b.flg = 'Y'
AND b.source = '1'
LEFT JOIN fac_app_data_table_field_val e ON a.id = e.val0
AND e.table_id = 1969241186793349598
AND e.school_id = '22'
AND e.flg = 'Y'
AND e.source = '1'
LEFT JOIN dictionary_info c ON a.val6 = c.id
AND c.schoolid = '22'
AND c.flg = 'Y'
LEFT JOIN dictionary_info d ON a.val18 = d.id
AND d.schoolid = '22'
AND d.flg = 'Y'
LEFT JOIN user_info ui ON a.val21 = ui.id
AND ui.schoolid = '22'
AND ui.flg = 'Y'
WHERE
a.table_id = 1969243196838052093
AND a.school_id = '22'
AND a.flg = 'Y'
AND a.source = '1'
AND a.val6 IN ('中标销售合同', '服务销售合同')
AND IF('blank' = '学校内涵式发展平台', 1 = 1, a.val3 LIKE concat('%', '学校内涵式发展平台', '%'))
GROUP BY
a.id
Problem: If c.val0 = a.id
is modified to CONVERT(c.val0, SIGNED) = a.id
, then record 1 can be correct, but record 2 will have an error. If it is changed back to c.val0 = a.id
, then record 2 can be correct, but record 1 will have an error.
id
is of bigint type, val0
is of varchar type.
【Resource Configuration】Enter TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page
【Attachments: Screenshots / Logs / Monitoring】