Mysql 左联接和未返回预期值
我有两个要加入的表,但查询有时在另一行中匹配 表t1 表2 查询 实际结果: 预期结果: 我所期望的是t2中的连接匹配,当Mysql 左联接和未返回预期值,mysql,Mysql,我有两个要加入的表,但查询有时在另一行中匹配 表t1 表2 查询 实际结果: 预期结果: 我所期望的是t2中的连接匹配,当Cla='RV'如下: +-----+-----+-----+ | Re | Re2 | Sum | +-----+-----+-----+ | 123 | abc | 15 | | 321 | zyx | 10 | +-----+-----+-----+ select t1.Re,t2.Re2, sum(t2.Importeenmonedalocal) as Su
Cla='RV'
如下:
+-----+-----+-----+
| Re | Re2 | Sum |
+-----+-----+-----+
| 123 | abc | 15 |
| 321 | zyx | 10 |
+-----+-----+-----+
select t1.Re,t2.Re2, sum(t2.Importeenmonedalocal) as Sum, from table t1
left join table2 as t2 on
t2.Re LIKE CONCAT ('%',t1.Re,'%')
where t2.Cla='RV'
group by t1.Re;
我尝试在其中添加一个,如下所示:
+-----+-----+-----+
| Re | Re2 | Sum |
+-----+-----+-----+
| 123 | abc | 15 |
| 321 | zyx | 10 |
+-----+-----+-----+
select t1.Re,t2.Re2, sum(t2.Importeenmonedalocal) as Sum, from table t1
left join table2 as t2 on
t2.Re LIKE CONCAT ('%',t1.Re,'%')
where t2.Cla='RV'
group by t1.Re;
但总数是不正确的:
+-----+-----+-----+
| Re | Re2 | Sum |
+-----+-----+-----+
| 123 | abc | 10 |
| 321 | zyx | 6 |
+-----+-----+-----+
你知道怎么做吗?选择t1.Re,
最大值(当t2.Cla='RV'然后t2.Re2结束时的情况)Re2,
SUM(t2.importeenmonedalcal)`SUM`
从`table`t1
在t2.Re上的左联接表2 t2类似于CONCAT(“%”,t1.Re“%”)
t1.Re分组;
im使用此查询连接表此查询是不确定的。例如-在第一个输出行中t2.Re2
有时可能是'abc'
,有时可能是'cba'
。。
select t1.Re,t2.Re2, sum(t2.Importeenmonedalocal) as Sum, from table t1
left join table2 as t2 on
t2.Re LIKE CONCAT ('%',t1.Re,'%')
where t2.Cla='RV'
group by t1.Re;
+-----+-----+-----+
| Re | Re2 | Sum |
+-----+-----+-----+
| 123 | abc | 10 |
| 321 | zyx | 6 |
+-----+-----+-----+