OrderNumber SKU IsFBA Description
123456 RED5 Product Payment
123456 RED5 commission
123456 RED5 FBA Fees
234567 RED5 Product Payment
234567 RED5 commission
345678 RED5 Product Payment
345678 RED5 commission
我想创建一个更新IsFBA = yes的SQL语句 在所有OrderNumbers中具有相同orderNumber的任何行,其中描述类似'%FBA%'
换句话说: 第3行,“描述”包含“FBA”(LIKE'%FBA%')。我希望在与此行具有相同OrderNumber的所有其他行中将'IsFBA'更新为yes。
所以我的结果是:
OrderNumber SKU IsFBA Description
123456 RED5 yes Product Payment
123456 RED5 yes commission
123456 RED5 yes FBA Fees
234567 RED5 Product Payment
234567 RED5 commission
345678 RED5 Product Payment
345678 RED5 commission
我可以通过PHP循环数据库轻松完成此操作,但想知道是否有一个mySQL语句可以做同样的事情。
答案 0 :(得分:1)
这应该有效,尚未经过测试。
UPDATE MyTable m1
INNER JOIN (
SELECT OrderNumber
FROM MyTable
WHERE Description LIKE '%FBA%'
GROUP BY OrderNumber
) m2 ON m1.OrderNumber = m2.OrderNumber
SET IsFBA = 'yes'
比使用IN
略好。
答案 1 :(得分:0)
您可以在子查询中选择OrderNumber
,如下所示:
UPDATE table SET IsFBA = true WHERE OrderNumber IN (SELECT DISTINCT OrderNumber FROM `table` WHERE Description LIKE '%FBA%')
答案 2 :(得分:0)
Update yourTable SET IsFBA = true WHERE OrderNumber IN
(SELECT DISTINCT OrderNumber FROM yourTable WHERE Description LIKE '%FBA%') AS A
答案 3 :(得分:0)
UPDATE table
SET IsFBA = true
WHERE OrderNumber IN (
SELECT OrderNumber
FROM table
WHERE Description LIKE '%FBA%')