A note for multiple table queries in MySQL union

Source: Internet
Author: User


Suppose there are two fields exactly consistent with the table, Table1 and table2. Fields are password, username, email.
The basic method for multiple table queries using union (or union All) is a simple connection:

SELECT Password,username,email
From
Table1
where username like ' 123 '

UNION ALL//or union to remove duplicate data

SELECT Password,username,email
From
Table2
where username like ' 123 '

However, if the table differs from the table's fields, there is overlap but not exactly the same. It is not simply a union to connect.
To union two or more tables, the conditions that must be met are the same fields for those tables. To satisfy this condition, you can use "," instead of the missing field.
For example, there is a field password,username in the Table1 field password,username,email,ip;table2. The Query method is as follows:


/**
* MySQL Union
* Coder:xiaohudie
* 2015
*/

SELECT Password,username,email,ip
From
Table1
where username like ' 123 '

UNION All

SELECT password,username, ', ', ', '
From
Table2
where username like ' 123 '
Analogy

--
PS: Sorry everyone, I withdrew the comment reply email notification function, I hope to know.
Because the SMTP plug-in left a mailbox password in the database poses a certain risk. As for why it is dangerous, cough, ask me a close friend.

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.