It may be an ignored update Statement (update technique) or an update statement

Source: Internet
Author: User

It may be an ignored update Statement (update technique) or an update statement

Well, I 've written a simple article again, and I hope you will not be enlightened. This time I want to talk about the update statement in SQL. Speaking of this, there will certainly be a lot of brothers who are not satisfied. Isn't it an update statement? No one will write it. update table set column = expression [where search_condition.

But is the update statement just like this? What is its principle? We use examples to illustrate:
Assume that there is a two-dimensional table with only two fields: Name and score. How can I use an SQL statement to query a student's name, score, and ranking in the table?
Ha ha, this problem is the last time I sent, details see http://www.bkjia.com/article/53832.htm
The result can be viewed there. Now, let's change the question. Assume that this two-dimensional table has three fields: name, score, and ranking. Now, only the names and scores have values. How can we update their rankings?

A friend may come up with a similar solution: first use the query statement to query and then use the cursor to update the ranking.
Is there a better solution? Yes, of course. The answer is to use the update statement.

Update orders table set ranking = (select count (*) + 1 from orders table where a. score <score)
From orders Table

We can see that only one statement is used. At the same time, we can also find that it is different from the update statement we usually write, with more from. In fact, the principle of the update statement is similar to that of the select statement. The complete update statement should be update table set column = expression from table [where search_condition]. it can be seen that the next part is a query statement. When the updated table and the queried table (that is, the table following the from) are completely consistent, we can omit it and write it as our most common update table set column = expression [where search_condition].

The above is just an example to illustrate the use of update. In actual development, flexible and appropriate use of update can achieve twice the result with half the effort.


For UPDATE statements

... I don't know what graphic interface you are using. Press enter to execute the statement, but it is certain that you didn't write it. If the statement cannot be executed, don't tell me that you are the first to write the statement; habits
That's why your worries are mostly worrying... normal people cannot make such ultra-low-level mistakes ..

How to UPDATE multi-row conditions using the UPDATE statement

Where student ID in ('1', '3', '5', '7 ')

You can.

Related Article

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.