O'Reilly logo

MySQL in a Nutshell by Russell J.T. Dyer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

SELECT

Synopsis

SELECT [flags] {column,  . . . |expressions}[,  . . . ] 

  FROM table[,  . . . ] 
  [WHERE condition]
  [GROUP BY {column|expression|position}[ASC|DESC],  . . . 
     [WITH ROLLUP]]
  [HAVING condition]
  [ORDER BY {column|expression|position}[ASC|DESC] ,  . . . ]
  [LIMIT {[offset,] count|count OFFSET offset}]
  [PROCEDURE procedure(arguments)]
  options

Use this statement to retrieve and display data from tables within a database. It has many clauses and options. However, for simple data retrieval many of them can be omitted. The basic syntax for the statement is shown. After the SELECT keyword, some flags may be given. Next, a list of columns to retrieve and/or expressions may be given, separated by commas. For the tables, all that is required is one or more table names from which to retrieve data in a comma-separated list. The remaining clauses may be called on to refine the data to be retrieved, to order it, and so forth. These various flags, options, and clauses are detailed in subsections to this statement explanation. Here is a simple example of how you can use this statement:

SELECT name_first, name_last, telephone_home,
       DATEDIFF(now( ), last_review)
       AS 'Days Since Last Review'
   FROM employees;

In this example, three columns and the results of an expression based on a fourth column are to be displayed. The first and last name of each employee, each employee's home telephone number, and the difference between the date of the employee's last employment review and the date now are listed. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required