select 其中一列是另一列的一部分

select where one column part of another

我想 select 我的 table 中的所有行,其中一列的值以另一列的值开头。

Table
column_a         column_b

abc              abcdef
pqr              rstrv
xyz              xyzabc
uvw              abcdef

我想得到

pqr              rstrv
uvw              abcdef

我正在使用查询

SELECT * FROM table_name WHERE column_b NOT LIKE column_a + '%'

我遇到了错误

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+ '%' LIMIT 0, 30' at line 1

我已经尝试过这些问题的解决方案 Compare Columns Where One is Similar to Part of Another

Oracle: LIKE where any part of one string matches amy part of another string

SQL search column where one item in column is substring of another item

SQL search column where one item in column is substring of another item Update。但是还是导致错误或者错误的结果

我的mysql版本是服务器版本:5.5.44-0ubuntu0.14.04.1 (Ubuntu)。 我正在使用 phpMyAdmin 来执行查询。

你的想法是对的,但是MySQL不支持字符串连接的+运算符,只支持数学加法。相反,只需使用 concat 函数:

SELECT * FROM table_name WHERE column_b NOT LIKE CONCAT(column_a, '%')

在MySQL中,你应该使用CONCAT()而不是+

select *
from TableName
where column_b not like CONCAT(column_a,'%')

在SQL服务器中:

select *
from TableName
where column_b not like column_a+'%'

尝试

SELECT * FROM table_name WHERE column_b NOT LIKE CONCAT(column_a,'%')

SELECT * FROM table_name WHERE column_b LIKE CONCAT('%', column_a, '%')