Multiple database Field names can be queried.

Source: Internet
Author: User


When querying multiple database Field names, the results are duplicated. How can I query duplicate database results? When using mysql, you sometimes need to query records with unique fields. Although mysql provides the keyword distinct to filter out redundant duplicate records, only one record is retained, but it is often used to return the number of records that do not repeat, instead of returning all values that do not record the record. Www.2cto.com the reason is that distinct can only return its target field, but cannot return other fields. This problem has plagued me for a long time. If distinct cannot solve this problem, I only use dual-loop query to solve the problem. This will undoubtedly directly affect the efficiency of a station with a large data volume. So I spent a lot of time studying this problem, and I couldn't find a solution on the Internet. During this period, I pulled Rong for help. As a result, both of us were depressed. Www.2cto.com let's take a look at the example below: The TABLE structure 'member' create table if not exists 'member' ('uid' int (7) not null AUTO_INCREMENT, 'username' varchar (8) default null, 'telphone' varchar (11) default null, primary key ('uid') ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 2003879; insert into 'member' ('uid', 'username', 'telphone') VALUES (2003875, 'qsdcz ', '123 '), (10034, 'asdf ', '1 3989775873 '), (10023, 'qwer', '000000'), (13989775873, 'edcvf', '000000'), (10039, '234jhkus', '20170 '), (10982, '234jhkus', '1234'), (18783493002, 'cfdfv', '1234'), (12234, NULL, '1234'), (13687879832, 'wertyuu ', '000000'), (12345678901, 'rtyuijkd', '000000'); the library structure is like this. This is just a simple example, and the actual situation is much more complicated. For example, if you want to use a statement to query all data with no duplicate names, you must use distinct to remove redundant duplicate records. The result of select distinct telephone from table telephone is: 12345678901 12345678902 13267864587 13687879832 13989775873 18701395130 18783493002 seems to have achieved the effect, but what do I want to get is the id value? Modify the query statement: select distinct telephone, uid, username from table: uid username telephone 2003875 qsdcz 12345678902 10034 asdf 13989775873 10023 qwer 13989775873 10039 edcvfr 13267864587 2003874 234 jhkus 18701395130 10982 jhkus 234 18783493002 12234 cfdfv 13687879832 2003876 sdf 12345678901 2003877 wertyuu 12345678901 2003878 rtyuijkd 12345678901 distinct didn't work? It works, but it also applies to two fields, that is, it must have the same id and name to be excluded. Let's modify the query statement www.2cto.com select id and distinct name from table. Unfortunately, you cannot get anything except the error information. distinct must be placed at the beginning. Is it difficult to place distinct in the where condition? Yes. An error is returned. What should I do? We use the function to calculate fields that do not need to be repeated. Actually succeeded. Now let out the complete statement: select *, count (distinct telephone) from table group by telephone result: uid username telephone 2003878 rtyuijkd 12345678901 2003875 qsdcz 12345678902 10039 edcvfr 13267864587 12234 cfdfv 13687879832 10023 qwer 13989775873 2003874 jhkus 234 18701395130 0982 jhkus 234. You can solve the problem.

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.