2017-02-15 41 views
0

表1表列:更新从另一个表列的最低值

ID | SKU | Images 
------------------ 
1 | 1502 | Link1 
2 | 1852 | Link2 
3 | 1745 | Link3 

表2:

ID | Product | MainLink 
------------------------ 
1 | 1501 | Link1 
2 | 1502 | Link1 
3 | 1852 | Link2 
3 | 1745 | Link3 

我试图从表1与各MainLink的表2中的最低值更新SKU

结果将是:

表1:

ID | SKU | Images 
------------------ 
1 | 1501 | Link1 
2 | 1852 | Link2 
3 | 1745 | Link3 

这是查询我有这么远,但ID不会让我的最低值:

UPDATE Table1 t1 JOIN Table2 t2 ON t1.Images = t2.MainLink SET t1.SKU = t2.Product 
+0

参见SQL的[ORDER BY](HTTPS: //www.w3schools.com/sql/sql_orderby.asp)关键字 – kman

回答

0

试试这个方法:

UPDATE Table1 t1 
JOIN (SELECT MIN(Product) Product, MainLink 
    FROM Table2 GROUP BY Table2.MainLink 
) t2 ON t1.Images = t2.MainLink SET t1.SKU = t2.Product 
+0

谢谢。我得到#1054 - 'on条款'中的未知列't2.MainLink' –

+0

我修复了它,现在我得到这个错误:#1062 - 重复'SKU'的重复条目'16998' –

+0

是的,但是这可以没有要做这个答案也许你有相同的Table2.Product惠特为两个不同的链接设置。然后当你更新Table1时,你对SKU有唯一的约束。 试试这个,看看你是否有多个记录 SELECT * FROM Table2 WHERE产品= 16998 – Galileox86

相关问题