SQL update table1.column1 if table1.column2 like table2.column2

时间:2014-08-10 21:04:21

标签: sql-update sql-like

我有两张桌子。产品P1和制造商M1。我想查看manufacturer_name是否在P1.title中,并使用P1.manufacturer_id更新M1.manufacturer_id

UPDATE products P1
set P1.manufacturer_id=manufacturers.manufacturer_id 
WHERE P1.title LIKE manufacturers.manufacturer_name;

我收到错误"#1054 - Unknown column 'manufacturers.manufacturer_name' in 'where clause' "

另外,WHERE P1.title LIKE %M1.manufacturer_name%的正确语法是什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

您没有发表过很多关于表结构或制造商与产品之间关系的信息,所以以下是我最好的猜测:请仔细检查并测试。我更喜欢用JOIN写这个,但不知道外键是什么,我不愿意冒险重复......

UPDATE P1
SET P1.Manufacturer_ID = 
  (
    SELECT TOP 1 Manufacturer_ID 
    FROM Manufacturers 
    WHERE P1.Title LIKE '%' + manufacturer_name + '%'
  )
FROM Products P1