MySQL is the most convenient way to go to the weight of the two

Source: Internet
Author: User

Reference: http://blog.csdn.net/guocuifang655/article/details/3993612

Method One:

When using MySQL, it is sometimes necessary to query for a record that does not duplicate a field, although MySQL provides the DISTINCT keyword to filter out redundant duplicate records to keep only one, but it is often used only to return the number of distinct records, instead of using it to return all values that are not re-recorded. The reason for this is that distinct can only return its target field and cannot return other fields

Let's take a look at the example below:

Table
ID Name
1 A
2 b
3 C
4 C
5 b

The library structure is probably like this, this is just a simple example, the actual situation will be much more complex.

For example, if I want to use a single statement to find all the data that name does not repeat, then you must use distinct to remove the redundant duplicate records.

Select DISTINCT name from table
The resulting results are:

Name
A
B
C

It seems to be working, but what I want to get is the ID value? Change the query statement:

Select DISTINCT name, ID from table

The result would be:

ID Name
1 A
2 b
3 C
4 C
5 b

Why doesn't distinct work? The role is up, but he also functions two fields, that is, the ID must be the same as the name will be excluded ....

Let's change the query statement:

Select ID, distinct name from table

Unfortunately, in addition to the error message you get nothing, distinct must be placed at the beginning. Is it difficult to put distinct in a where condition? Can, still error ....

The final useful statement is as follows:

SELECT *, COUNT (distinct name) from the table group by name

Results:

ID Name count (distinct name)
1 a 1
2 B 1
3 C 1

The last item is superfluous, do not care on the line, the purpose to achieve ...

Oh, yes, and by the way, group by must be placed before order by and limit, otherwise it will error ...! Okay, OK.

Summary statement:SELECT *, COUNT (distinct name) from (SELECT * FROM table ...) such as nested statements) group by name

Method Two:

Using GROUP BY

[SQL]View Plaincopy
  1. SELECT * from (
  2. SELECT * FROM customer where user= (
  3. Select Source_user from customer WHERE user=' admin ') UNION all SELECT * from customer where user= (
  4. Select Source_user from customer where user= (
  5. Select Source_user from customer WHERE user=' admin ')] union all SELECT * from custome R where user= (
  6. Select Source_user from customer where user= (
  7. Select Source_user from customer where user= (
  8. Select Source_user from customer WHERE user=' admin ')) UNION all SELECT * from cus Tomer where source_user= (/* My on-line on-line user*/
  9. Select User from customer where user= (
  10. Select Source_user from customer where user= (
  11. SELECT Source_user from customer WHERE user=' admin ')) UNION ALL SELECT * from Custom Er where source_user= (/* My on-line online user*/
  12. Select User from customer where user= (
  13. Select Source_user from customer where user= (
  14. Select Source_user from customer where user= (
  15. SELECT Source_user from customer WHERE user=' admin ')))))  As alias group by user;

Note Add aliases, or error, note the WHERE statement outside the packaging, and then use Group by to go to the weight to take effect.

MySQL is the most convenient way to go to the weight of the two

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.