gpt4 book ai didi

LIKE 查询中的 MySQL 列值

转载 作者:行者123 更新时间:2023-11-29 05:28:10 25 4
gpt4 key购买 nike

我在数据库中有两个表。

两者都包含一个相关的 id,但格式不同:

table1.field = 123456-12-34
table2.id = 123456

我想做的是 table1.id = table2.field 上的 join

所以查询看起来像:

select name from 
table1 left join table2 on table1.field like table2-%
where table2.flag='1' and DATEDIFF( now(), table1.timestamp ) > 2

我知道这是不正确的,但我如何在相关但不相同的字段上连接两个表?

最佳答案

您想使用 concat() 来创建模式字符串:

select name
from table1 left join
table2
on table1.field like concat(table2.id, '-%')
where table2.flag='1' and DATEDIFF( now(), table1.timestamp ) > 2

关于LIKE 查询中的 MySQL 列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17633236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com