Rechercher dans le manuel MySQL

3.3.4.3 Selecting Particular Columns

If you do not want to see entire rows from your table, just name the columns in which you are interested, separated by commas. For example, if you want to know when your animals were born, select the name and birth columns:

  1. mysql> SELECT name, birth FROM pet;
  2. +----------+------------+
  3. | name     | birth      |
  4. +----------+------------+
  5. | Fluffy   | 1993-02-04 |
  6. | Claws    | 1994-03-17 |
  7. | Buffy    | 1989-05-13 |
  8. | Fang     | 1990-08-27 |
  9. | Bowser   | 1989-08-31 |
  10. | Chirpy   | 1998-09-11 |
  11. | Whistler | 1997-12-09 |
  12. | Slim     | 1996-04-29 |
  13. | Puffball | 1999-03-30 |
  14. +----------+------------+

To find out who owns pets, use this query:

  1. mysql> SELECT owner FROM pet;
  2. +--------+
  3. | owner  |
  4. +--------+
  5. | Harold |
  6. | Gwen   |
  7. | Harold |
  8. | Benny  |
  9. | Diane  |
  10. | Gwen   |
  11. | Gwen   |
  12. | Benny  |
  13. | Diane  |
  14. +--------+

Notice that the query simply retrieves the owner column from each record, and some of them appear more than once. To minimize the output, retrieve each unique output record just once by adding the keyword DISTINCT:

  1. mysql> SELECT DISTINCT owner FROM pet;
  2. +--------+
  3. | owner  |
  4. +--------+
  5. | Benny  |
  6. | Diane  |
  7. | Gwen   |
  8. | Harold |
  9. +--------+

You can use a WHERE clause to combine row selection with column selection. For example, to get birth dates for dogs and cats only, use this query:

  1. mysql> SELECT name, species, birth FROM pet
  2.        WHERE species = 'dog' OR species = 'cat';
  3. +--------+---------+------------+
  4. | name   | species | birth      |
  5. +--------+---------+------------+
  6. | Fluffy | cat     | 1993-02-04 |
  7. | Claws  | cat     | 1994-03-17 |
  8. | Buffy  | dog     | 1989-05-13 |
  9. | Fang   | dog     | 1990-08-27 |
  10. | Bowser | dog     | 1989-08-31 |
  11. +--------+---------+------------+

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-selecting-columns.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut