0

我正在寻找创建 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的所有行,以Asku. 但是,我只想对没有唯一option_id值的行执行此操作。

例如,您可以看到option_id= 139 的行是唯一具有此option_id值的行,因此我想排除更新此行。

Basically this is a list of product options, some products have multiple options each with it's own SKU. However for these products that have multiple options there is an 'A' missing from the end of the 1st option's SKU, and I'd like to add it. But I don't wish to add an 'A' to products that only have 1 option. I hope this makes sense.

If someone could advise on the MySQL statement that could achieve this, that would be great!

4

2 回答 2

0

You can use a sub-query to limit to only those option_ids that have a count of 1.

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
于 2013-08-28T19:09:50.970 回答
0

This query will do what you want :

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

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

于 2013-08-28T19:19:32.257 回答