Get the App
SLTechnology News&Howtos  ›  Database  › 

SQL to repeat the query

Shulou Source: shulou.com Published: 2022-06-01 09:32:00 10月01日 Update

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

Tags: Xiaobai query Xiao Ming fields math Chinese Xiao Hong results English information individual numbers tables that is history grades politics numerical values data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Huawei OPPO Reno NVidia