Mysql Statement (query before insert)

Source: Internet
Author: User
The data table is as follows: {code ...} for example, if we know a7 and b8 but do not know the value of parent, We need to query the data table that has a7 & amp; b8 before inserting, or the parent value of the record of a8 & amp; b7 (ensure that the data table contains a7 & amp; b8... the data table is as follows:

id    parent    a      b    others---------------------------------------1        0      7      8---------------------------------------2        1      8      7---------------------------------------3        1      7      8---------------------------------------4        1      8      7---------------------------------------

For example, if a = 7 and B = 8 are known, but the parent value is unknown, the data table must be queried before being inserted.a=7 && b=8Ora=8 && b=7The parent value of the record (ensure that the data table meetsa=7 && b=8, Ora=8 && b=7).
How can I write SQL statements? Select first and then insert? Can I fix a statement?

Reply content:

The data table is as follows:

id    parent    a      b    others---------------------------------------1        0      7      8---------------------------------------2        1      8      7---------------------------------------3        1      7      8---------------------------------------4        1      8      7---------------------------------------

For example, if a = 7 and B = 8 are known, but the parent value is unknown, the data table must be queried before being inserted.a=7 && b=8Ora=8 && b=7The parent value of the record (ensure that the data table meetsa=7 && b=8, Ora=8 && b=7).
How can I write SQL statements? Select first and then insert? Can I fix a statement?

@ Eapen refer to my comments on your answers.
@ Xaero I think your problem has taken us all wrong.
The parent attribute does not exist for the first message because the messages are arranged in chronological order. Only Forum posts reply to messages with parent issues. This parent can be used for display at the front-end, and relevant discussions can be concentrated for ease of reading, this structure breaks the posting time rule, so you need to specify an additional parent.
Obviously, the message does not have this problem. Even if your message is generated for a specific topic, it should be reference_id rather than parent.

What do you think?

I understand what you mean. This can be maintained through another table.
Table message_groups
Group_id users
// Information exchange, sorted by user ID from small to large, separated by commas
2, 7, 9
...
Table messages
Group_id from_user to_user message time
1 7 8 Test1 ....
1 8 7 Re: Test1 ....

Do you think this is helpful?

IF EXISTS (SELECT * FROM Table_Name WHERE (a='7' and b='8') OR (a='8' and b='7'))BEGIN    --EXISTENDELSEBEGIN    INSERT INTO Table_Name(Column,...) VALUES('')END

I don't know why I don't know the value of parent? When reading the list, you should know the value of id. You can simply include the value when writing it.

The private message function of oschina is like this. It is clearly an existing user session. If I don't "reply", click "send", and the original session will also be summarized... I don't know how to implement it .... There is no answer to all the irrelevant chats on oschina.

You can write stored procedures to solve your problems perfectly.

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.