Use the MySQL DISTINCT clause to remove duplicate rows from the result set returned by the SELECT clause.Code language: plaintext ( plaintext ) Summary Without the DISTINCT clause, you will get the duplicate combination of state and city as follows: SELECT When you specify multiple columns in the DISTINCT clause, the DISTINCT clause will use the combination of values in these columns to determine the uniqueness of the row in the result set.įor example, to get a unique combination of city and state from the customers table, you use the following query: SELECT DISTINCTĬity Code language: SQL (Structured Query Language) ( sql ) When you use the DISTINCT clause to query the states, you will see distinct states and NULL as follows: SELECT DISTINCT stateįROM customers Code language: SQL (Structured Query Language) ( sql )ġ9 rows in set (0.00 sec) Code language: plaintext ( plaintext ) MySQL DISTINCT with multiple columns When you specify a column that has NULL values in the DISTINCT clause, the DISTINCT clause will keep only one NULL value because it considers all NULL values are the same.įor example, the state column in the customers table has NULL values. +-+Ĭode language: plaintext ( plaintext ) MySQL DISTINCT and NULL values Second, select unique last names by adding the DISTINCT clause like this: SELECT DISTINCT lastnameĪs you can see clearly from the output, the DISTINCT clause removes the duplicate last names from the result set. En este ejemplo de la sentencia MySQL SELECT, hemos utilizado para indicar que deseamos seleccionar todos los campos de la tabla categora. Veamos cmo usar una consulta MySQL SELECT para seleccionar todos los campos de una tabla. Lastname Code language: SQL (Structured Query Language) ( sql )Ģ3 rows in set (0.00 sec) Code language: plaintext ( plaintext )Īs shown clearly in the output, some employees have the same last names e.g., Bondur,Firrelli. Recuperar campos desde dos o ms tablas MySQL. If you must have a table alias name with the literal value, you need to wrap it in a subquery with the same name as the table you want to use: SELECT field1, field2, field3 FROM / subquery wraps all fields to put the literal. We’ll use the employees table from the sample database:įirst, select the last names from the employees table using the following SELECT statement: SELECT MySQL is usually configured to honor double quotes for strings, but single quotes are more widely portable among RDBMS. When executing the SELECT statement with the DISTINCT clause, MySQL evaluates the DISTINCT clause after the FROM, WHERE, and SELECT clause and before the ORDER BY clause: However, if you specify two or more columns, the DISTINCT clause will use the values of these columns to evaluate the uniqueness of the rows. If you specify one column, the DISTINCT clause will evaluate the uniqueness of rows based on the values of that column. In this syntax, you specify one or more columns that you want to select distinct values after the SELECT DISTINCT keywords. Sort_expression Code language: SQL (Structured Query Language) ( sql ) Here’s the syntax of the DISTINCT clause: SELECT DISTINCT To remove these duplicate rows, you use the DISTINCT clause in the SELECT statement. When querying data from a table, you may get duplicate rows. Summary: in this tutorial, you will learn how to use the MySQL DISTINCT clause in the SELECT statement to eliminate duplicate rows in a result set.
0 Comments
Leave a Reply. |