更新MySQL Query,选择具有特定非唯一值的记录

时间:2013-08-28 18:54:37

标签: mysql sql

我正在寻找帮助创建MySQL查询。我有以下数据库表:

+----------------+-----------+-------------+------------+-------------------+---------+-------------+---------------+--------+
| option_type_id | option_id | sku         | sort_order | customoptions_qty | default | in_group_id | dependent_ids | weight |
+----------------+-----------+-------------+------------+-------------------+---------+-------------+---------------+--------+
|            552 |       137 | 13071727    |       1000 | 0                 |       0 |           0 |               | 0.0000 |
|            553 |       137 | 13071727B   |       1000 | 0                 |       0 |           1 |               | 0.0000 |
|            554 |       137 | 13071727C   |       1000 | 1                 |       0 |           2 |               | 0.0000 |
|            555 |       137 | 13071727D   |       1000 | 1                 |       0 |           3 |               | 0.0000 |
|            556 |       138 | 13085350-1  |       1000 | 0                 |       0 |           0 |               | 0.0000 |
|            557 |       138 | 13085350-1D |       1000 | 2                 |       0 |           1 |               | 0.0000 |
|            558 |       138 | 13085350-1C |       1000 | 3                 |       0 |           2 |               | 0.0000 |
|            559 |       138 | 13085350-1B |       1000 | 2                 |       0 |           3 |               | 0.0000 |
|            560 |       139 | 13069547M   |       1000 | 20                |       0 |           0 |               | 0.0000 |
|            561 |       140 | 13084477-2  |        950 | 2                 |       0 |           0 |               | 0.0000 |
|            562 |       140 | 13084477-2B |        951 | 2                 |       0 |           1 |               | 0.0000 |
|            563 |       140 | 13084477-2C |        952 | 3                 |       0 |           2 |               | 0.0000 |
|            564 |       140 | 13084477-2D |        953 | 0                 |       0 |           3 |               | 0.0000 |
|            565 |       140 | 13084477-2E |        954 | 2                 |       0 |           4 |               | 0.0000 |
|            566 |       141 | 13066533-1  |       1000 | 4                 |       0 |           0 |               | 0.0000 |
|            567 |       141 | 13066533-1B |       1000 | 5                 |       0 |           1 |               | 0.0000 |
|            568 |       141 | 13066533-1C |       1000 | 5                 |       0 |           2 |               | 0.0000 |
|            569 |       141 | 13066533-1D |       1000 | 0                 |       0 |           3 |               | 0.0000 |
|            570 |       142 | 13071674    |       1000 | 0                 |       0 |           0 |               | 0.0000 |
|            571 |       142 | 13071674D   |       1000 | 1                 |       0 |           1 |               | 0.0000 |
|            572 |       142 | 13071674C   |       1000 | 2                 |       0 |           2 |               | 0.0000 |
|            573 |       142 | 13071674B   |       1000 | 0                 |       0 |           3 |               | 0.0000 |
|            574 |       142 | 13071674E   |       1000 | 4                 |       0 |           4 |               | 0.0000 |
|            575 |       143 | 13071667    |       1000 | 0                 |       0 |           0 |               | 0.0000 |
|            576 |       143 | 13071667B   |       1000 | 0                 |       0 |           1 |               | 0.0000 |
|            577 |       143 | 13071667C   |       1000 | 0                 |       0 |           2 |               | 0.0000 |
|            578 |       143 | 13071667D   |       1000 | 0                 |       0 |           3 |               | 0.0000 |
|            579 |       143 | 13071667E   |       1000 | 0                 |       0 |           4 |               | 0.0000 |
|            580 |       144 | 13066295    |       1000 | 1                 |       0 |           0 |               | 0.0000 |
+----------------+-----------+-------------+------------+-------------------+---------+-------------+---------------+--------+

我想要做的是更新 in_group_id = 0的所有行,将A添加到sku的末尾。但是,我只想在具有唯一option_id值的行上执行此操作。

例如,您可以看到option_id = 139的行是唯一行,其option_id值为{<1}},因此我想排除此行未更新。

基本上这是一个产品选项列表,有些产品有多个选项,每个选项都有自己的SKU。然而,对于具有多个选项的这些产品,第一个选项的SKU末尾缺少“A”,我想添加它。但我不希望在只有1个选项的产品中添加“A”。我希望这是有道理的。

如果有人可以就可以实现这一目标的MySQL声明提出建议,那就太棒了!

2 个答案:

答案 0 :(得分:0)

您可以使用子查询来限制那些计数为1的option_ids。

UPDATE  YourTable T
INNER JOIN
        (SELECT option_id
                ,COUNT(option_id) as opt_count
        FROM    YourTable T1
        GROUP BY
                option_id
        ) SQ
ON      SQ.option_id = T.option_id                
SET     T.sku = CONCAT(T.sku,'A') 
WHERE   T.in_group_id = 0 
AND     SQ.opt_count = 1

答案 1 :(得分:0)

此查询将执行您想要的操作:

UPDATE Table1
LEFT OUTER JOIN (SELECT *
           FROM Table1
        GROUP BY option_id
        HAVING count(*) = 1) t1 ON t1.option_type_id = Table1.option_type_id
SET table1.sku = concat(table1.sku,'A')
WHERE table1.in_group_id = 0 AND t1.option_type_id is null

请参阅SQL FIDDLE:http://www.sqlfiddle.com/#!2/9d08e/1/0