我想从表中选择所有行,其中一列的值以另一列的值开头。
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-您的SQL语法有错误;请查看对应于MySQL服务器版本的手册,以获取在第1行“+”%”限制0,30”附近使用的正确语法
我试过从这些问题中找到解决办法
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, '%')