如何在左连接语句上使用替换

how to use replace on a left join statement

我有两个表,其中一个有一个字段,其中包含类似“1111AAA”的字符串,另一个有相同的字段,但结构为“1111 AAA”。我想把空格space换成'',但是我在left join.

的ON附近报错

我把代码放在下面:

select idticket, bt.matricula, bv.vehicle
from b_ticket bt
left JOIN b_vehicle bv ON REPLACE(bv.matricula, ' ', '') ilike ON REPLACE(bt.matricula, ' ', '')
where date_start >= '2019/01/01/'
and date_end <= '2020/01/01'

我无法更改两个表中任何一个的值。我也尝试 TRIM 函数,但它不起作用,因为它删除了字符串开头和结尾的空白 spaces,而不是单词之间的空白。

有什么想法吗?

谢谢!

错误信息: 错误:靠近 «ON» 的语法错误 第 3 行:... bv ON REPLACE(bv.matricula, ' ', '') ilike ON REPLACE...

您的 JOIN 子句中很可能有一个无关的 ON 关键字:

SELECT
    idticket, bt.matricula, bv.vehicle
FROM
    b_ticket AS bt LEFT JOIN b_vehicle AS bv
        ON REPLACE(bv.matricula, ' ', '') ILIKE REPLACE(bt.matricula, ' ', '')
WHERE
    date_start >= '2019/01/01/' AND date_end <= '2020/01/01';

如错误所述:谓词必须是任何有效的布尔表达式,但其中不能包含 ON 关键字。

这是无效的SQL:

left JOIN b_vehicle bv 
    ON REPLACE(bv.matricula, ' ', '') ilike ON REPLACE(bt.matricula, ' ', '')

每个 JOIN 应该只有一个 ON。据推测,你想要:

left JOIN b_vehicle bv 
    ON REPLACE(bv.matricula, ' ', '') ilike REPLACE(bt.matricula, ' ', '')