In mysql, the distinct keyword is used to filter duplicate data. It can filter duplicate data in one or more fields in the data table and only return one piece of data to the user; the distinct keyword needs to be Used together with the SELECT statement, the syntax is "SELECT DISTINCT column1, column2, ... FROM table_name;".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
When using the SELECT statement to perform a simple data query in MySQL, all matching records are returned. If some fields in a table do not have unique constraints, duplicate values may exist in these fields. In order to query unique data, MySQL provides the DISTINCT keyword.
The main function of the DISTINCT keyword is to filter duplicate data in one or more fields in the data table, and only return one piece of data to the user.
The syntax format of the DISTINCT keyword is:
SELECT DISTINCT column1, column2, ... FROM table_name;
where "column
" is the name of one or more fields that need to eliminate duplicate records , separate multiple fields with commas.
You need to pay attention to the following points when using the DISTINCT keyword:
The DISTINCT keyword can only be used in a SELECT statement.
When deduplicating one or more fields, the DISTINCT keyword must be at the front of all fields.
If there are multiple fields after the DISTINCT keyword, the multiple fields will be combined and deduplicated. That is to say, only when the combination of multiple fields is exactly the same. will be deduplicated.
Demo Database
The following is a selection of the "Customers" table in the Northwind sample database:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constituciуn 2222 | Mйxico D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquerнa | Antonio Moreno | Mataderos 2312 | Mйxico D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbkцp | Christina Berglund | Berguvsvдgen 8 | Luleе | S-958 22 | Sweden |
No SELECT instance using DISTINCT
The following SQL statement selects ALL (including duplicates) values from the "Country" column in the "Customers" table:
SELECT Country FROM Customers;
##SELECT DISTINCT instance
1. Select DISTINCT values only from the "Country" column in the "Customers" table:SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;
mysql video tutorial]
The above is the detailed content of How to use mysql distinct. For more information, please follow other related articles on the PHP Chinese website!

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools
