Parse MySQL: Single-table distinct, multi-table group by query to remove duplicate records

Source: Internet
Author: User
Tags mysql manual

single-table only query by: DISTINCT
Unique query for multiple tables: Group by
Distinct when querying multiple tables, the LEFT join is valid and the full connection is invalid.
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, rather than using it to return all values of the non-repeating record. The reason is that distinct can only return its target field, and can not return other fields, with distinct can not solve, I only use a double-loop query to solve, and so for a very large number of stations, will undoubtedly directly affect the efficiency.
Let's take a look at the example below:
The structure of the table is as follows:
ID Name
1 A
2 b
3 C
4 C
5 b
The basic table structure is probably the case, this is just a simple example, the actual multi-table query and so on, and so the situation is 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

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 actually up, but he also functions two fields, that is, must have the same ID and 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? Try it and give an error.

Tried for a half day other can think of method also not, finally found a usage in MySQL manual, with group_concat (distinct name) with group by name to achieve the function I need, excited, God bless me also, quickly try.
Error, Depressed!
Even the MySQL handbook with me, first gave me hope, and then pushed me to despair.
After a closer look, the Group_concat function is 4.1 supported, Halo, I am 4.0. No way, upgrade, finish a test, success.
Finally, but then, you have to ask customers to upgrade.
Suddenly the spirit flashes, since you can use the Group_concat function, can the other functions be OK?
Hurriedly with the Count function a try, success, cost so much effort, originally so simple.
now release the full statement:
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 is achieved.
The original MySQL so stupid, gently put him to cheat, now take out hope you don't be this problem toss.
By the way, group by must be placed before order by and limit, otherwise the error will be reported.
say the actual example of GROUP by:

 $sql =   "  Select DISTINCT N.nid,tn.tid,n.title,n.created,ni.thumbpath from {Term_node} tn INNER JOIN {node} n on N.nid=tn.nid INNER JOIN {node_images} ni on Ni.nid=n.nid where Tn.tid in (  ". Implode ( " ,  , $tids). Span style= "COLOR: #ff0000" > " ) ORDER by N.nid DESC  "   =   Db_query ($sql), $t _data  =   Array ();  while  ($r =   Db_fetch_array ($res)) {Print_r ($r);}  

When using this query, there will always be two of the same nid, such as the following results

Array ([created] = 1215331278[nid] = 1603[Tid] =  +[title] =Summer wedding green drink DIY[Thumbpath] =Files/Node_images/Home- the. 1_tn.jpg) Array ([created] = 1215331278[nid] = 1603[Tid] =  +[title] =Summer wedding green drink DIY[Thumbpath] =Files/Node_images/003_primary_tn.jpg)

The above use of distinct also no use, in fact, it is useful, but I would like to query the structure of the NID is unique.
Finally, GROUP by

= ' Selectn.nid,tn.tid,n.title,n.created,ni.thumbpath from {Term_node} tn innerjoin {node} n on N.nid=tn.nid INNER JOIN { Node_images} ni Onni.nid=n.nid where Tn.tid in ('. Implode (',', $tids). ' GROUP byn.nid DESC'==  Array ();  while = Db_fetch_array ($res)) {Print_r ($r);}

I got the NID to be the only one.

Parsing MySQL: Single-table distinct, multi-table group by query removal of duplicate records

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.