Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SQL to repeat the query

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

SQL single or group to repeat query method

The following is the data used for testing:

Table name: t_Demo

IDNameProjectGrade1 Xiaoming Mathematics 591 Xiaoming Chinese 591 Xiaoming English 592 Xiaoxing Mathematics 1002 Xiaohong Chinese 993 Xiaobai Mathematics 1003 Xiaobai Chinese 903 Xiaobai English 803 Xiaobai History 604 Xiaoming Math 894 Xiaoli Chinese 894 Xiaohei English 90

1. A single field to repeat the query

We now want to get the ID field information table in the above table and remove the duplicate values, that is, we now want to get how much ID there is in the table.

Select id from t_Demo group by id

Query results:

ID1234

two。 Multiple fields to repeat the query

If you need to get ID and Name and remove the duplicate value information table, it is equivalent to getting how many people are in the current table.

Select id,Name from t_Demo group by id,Name

Query results:

IDName1 Xiao Ming 2 Xiao Hong 3 Xiao Bai 4 Xiao Hei

3. Multiple fields to repeat and get the number of repetitions

Now we need to get all the scores of each person in the table.

Select ID,Name,count (distinct Project) as num from t_Demo group by ID,Name

Query results:

ID

Namenum1 Xiaoming 32 Xiao Hong 23 Xiao Bai 54 Xiao Hei 3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report