比较两列的值 [英] Compare values of two columns
本文介绍了比较两列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以比较MySQL中两列的值?例如,如果我有一张桌子:
Is there a way to compare the values of two columns in MySQL? For example if I have a table:
+----------------+
| Col1 | Col2 |
|----------------|
| abc | 123abc |
| def | 234def |
| | 123ghi |
+----------------+
我想检索Col2
中包含Col1
值的所有条目:
And I wanted to retrieve all the entries in Col2
that contained the values of Col1
:
+---------+
| NewCol |
|---------|
| 123abc |
| 234def |
+---------+
我该怎么办?
这是一个伪查询,以进一步解释.
Here is a pseudo-query to explain a bit further.
SELECT Col2 FROM TableName WHERE Col2 LIKE Col1;
推荐答案
使用 LOCATE()
WHERE LOCATE(Col1, Col2);
如果Col2
中包含Col1
,则返回非零值.
It returns a non-zero value if Col1
is contained within Col2
.
请注意,另一个字符串中始终包含一个空的子字符串,因此在这种情况下,您需要另一个条件:
Note that an empty substring is always contained within another string, so in this case you need another condition:
WHERE LENGTH(Col1) AND LOCATE(Col1, Col2);
这篇关于比较两列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文