开发者

MySQL query using update or select statement

We have a table as follows:

CREATE TABLE client_service_details(
  id bigint(20) NOT NULL AUTO_INCREMENT,
`client_details_id` bigint(20) NOT NULL,
`service_id` bigint(20) NOT NULL,
`list_index`  int(10)  NOT NULL default 0 ,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB ;


+----+-------------------+------------+------------+
| id | client_details_id | service_id | list_index |
+----+-------------------+------------+------------+
|  1 |               101 |          1 |          0 |
|  2 |               101 |          2 |          0 |
|  3 |               101 |          3 |          0 |
|  4 |   开发者_开发技巧            102 |          1 |          0 |
|  5 |               102 |          2 |          0 |
|  6 |               102 |          3 |          0 |
|  7 |               102 |          4 |          0 |
|  8 |               103 |          1 |          0 |
|  9 |               103 |          2 |          0 |
+----+-------------------+------------+------------+
9 rows in set (0.00 sec)

WE want to update the column (list_index) based on the client_details_id, servce_id. Or if we we get the select statement as give below the it is okay

The output should be as follows:

+----+-------------------+------------+------------+
| id | client_details_id | service_id | list_index |
+----+-------------------+------------+------------+
|  1 |               101 |          1 |          0 |
|  2 |               101 |          2 |          1 |
|  3 |               101 |          3 |          2 |
|  4 |               102 |          1 |          0 |
|  5 |               102 |          2 |          1 |
|  6 |               102 |          3 |          2 |
|  7 |               102 |          4 |          3 |
|  8 |               103 |          1 |          0 |
|  9 |               103 |          2 |          1 |
+----+-------------------+------------+------------+


UPDATE client_service_details
SET list_index = service_id -1

this should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜