"SQL Tips" MySQL modification, dynamically specifies the field to be modified Update ' table ' Set (case time ...) = 1 WHERE id = XX

Source: Internet
Author: User

If you have entered this post, then you must have encountered the same problem as me. Do not look at the set case of the topic when ..., I started with the first reaction is to use case when but found not to work.

It's a little bit bigger than that: dynamic specifies the field to modify.

In fact, little white here did not find the solution I thought [laughter cry], but at least the problem is solved.

Here are the addresses of the original posts:

Http://stackoverflow.com/questions/4830191/t-sql-using-a-case-in-an-update-statement-to-update-certain-columns-depending-o

Here to record the method, in fact, you update all of the values you might want to update, when updating the value of the field to determine whether to update, do not update the original value is attached, updated with the new value. [Laugh and Cry] [Laugh and Cry] [Laugh and Cry] [Laugh and Cry] [Laugh and Cry]

The final finished SQL is as follows: (the requirement background is that when sender_id equals the userid passed in, the update send_status is 2; when receiver_id equals UserID, the update receiver_status is 2)

UPDATEmessageSETSend_status=( Case  whensender_id=#{userid, Jdbctype=BIGINT} Then 2 ELSESend_statusEND), Receive_status=( Case  whenreceiver_id=#{userid, Jdbctype=BIGINT} Then 2 ELSEReceive_statusEND)WHEREdialog_idinch (<foreach Collection="List" item="Item"Index="Index"Separator=",">#{item, Jdbctype=BIGINT}</Foreach>)

I use the MyBatis, batch modification.

At work, hoping to help people who are equally confused.

"SQL Tips" MySQL modification, dynamically specifies the field to be modified Update ' table ' Set (case time ...) = 1 WHERE id = XX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.