Ideal logic and business logic

Source: Internet
Author: User

Today does an SQL statement, which is disgusting.

The requirement is also quite simple and common, that is, to compare whether a form has been changed.

If there is any change, send a mail. If not, the application is approved directly.

 

You can understand this.

There is no suspense in the general practice. For example, if the name of A is a backup table, let's make a PREA. Does a make a backup in PREA every time it is updated.

 

The problem is: we need to compare a record in a with a record in PREA, Which is troublesome.

One by one?

No, that's too tired. Someone gave me an idea:

"For the difference between set a and Set B (remove the same item), if set a and Set B get the difference set, then set B and set a get the difference set, and then merge, that's all."

 

This is probably the case with SQL:

(
Select   *   From A
Except
Select   *   From PREA
)
Union   All
(
Select   *   From PREA
Except
Select   *   From A
)

Then I made such a view, for example, diffofa.

If you look up a record in this view, it means there is a difference! If not, does it mean there is no difference.

The application is not bad. But the problem was quickly tested:

When there is no record in PREA, So !!! At this time, it will be considered as a difference...

=. =

Yes, there is no problem at all from the collection point of view, but it is also strange from the business point of view.

 

In fact, in the system I have encountered, there are two situations that will lead to no value in the pre. When a table is still located, that is, when it is inserted for the first time, there is no value in the pre.

The other is when the process ends. In this case, the PRE table is cleared. Because it has no value.

 

So this problem is well solved. Change to Pre and A to calculate the difference set. That is, the SQL statement is killed in half and becomes:

Select * FromPREA
Except
Select * FromA

..

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.