使用LIKE%...的Mysql查询出错

时间:2016-04-23 08:43:06

标签: mysql sql

我正在尝试使用另一个表更新表。我无法在此处确定如何使用LIKE%...%功能。请帮帮我。

表1:

TableToBeUpdated:

id | location | value
------------
1  | california | I am going to be here soon.
2  | Nill | Hello I love playing chess and yes.
3  | Nill | my hotel room is just shitty!
4  | Nill | Why on earth God doesn’t live on earth!
5  | Nill | friends of friends and their dogs.

表2:

TableToCheckFrom:

uniqueid | location | keyword
---------------------
1        | Texas | Why on earth
2        | NewYork   | friends and their
3        |   Washington | love playing chess
4        | NewYork   | their dogs

结果应为:

id | location | value
------------
1  | California | I am going to be here soon.
2  | Washington | Hello I love playing chess and yes.
3  | Nill | my hotel room is just shitty!
4  | Texas | Why on earth God doesn’t live on earth!
5  |NewYork| friends of friends and their dogs.

-

我正在使用这个公式,但它给了我不断的错误:

UPDATE TableToBeUpdated, TableToCheckFrom
SET TableToBeUpdated.location = TableToCheckFrom.Location
WHERE TableToBeUpdated.Value LIKE %TableToCheckFrom.Keyword%

提前致谢!

2 个答案:

答案 0 :(得分:1)

您需要将%放在引号中并将它们连接到关键字。并且您需要连接两个表,以便可以引用它们中的列。

UPDATE TableToBeUpdated AS u
JOIN TableToCheckFrom AS c ON u.Value LIKE CONCAT('%', c.Keyword, '%')
SET u.location = c.Location

答案 1 :(得分:0)

您查询错误(您永远不会告诉DBM您要访问TableToCheckFrom表)。 有关如何编写此类查询的想法,请查看此问题 Update mysql table with data from another table

如果您发现类似%%作为额外的困难条带:首先使用简单条件准备一个工作查询,然后添加类似条件。