Search the MySQL manual:

6.4.1 SELECT Syntax

SELECT [STRAIGHT_JOIN]
       [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
       [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY]
       [DISTINCT | DISTINCTROW | ALL]
    select_expression,...
    [INTO {OUTFILE | DUMPFILE} 'file_name' export_options]
    [FROM table_references
      [WHERE where_definition]
      [GROUP BY {unsigned_integer | col_name | formula} [ASC | DESC], ...
        [WITH ROLLUP]]
      [HAVING where_definition]
      [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]
      [LIMIT [offset,] row_count | row_count OFFSET offset]
      [PROCEDURE procedure_name(argument_list)]
      [FOR UPDATE | LOCK IN SHARE MODE]]

SELECT is used to retrieve rows selected from one or more tables. Each select_expression indicates a column you want to retrieve. SELECT may also be used to retrieve rows computed without reference to any table. For example:

mysql> SELECT 1 + 1;
         -> 2

All clauses used must be given in exactly the order shown above. For example, a HAVING clause must come after any GROUP BY clause and before any ORDER BY clause.

Subsections

User Comments

Posted by Colin Nelson on Wednesday February 26 2003, @2:10am[Delete] [Edit]

You can simulate a CROSSTAB by the following method:-

Use IF function to select the key value of the sub table as in:

SELECT
SUM(IF(beta_idx=1, beta_value,0)) as beta1_value,
SUM(IF(beta_idx=2, beta_value,0)) as beta2_value,
SUM(IF(beta_idx=3, beta_value,0)) as beta3_value
FROM alpha JOIN beta WHERE alpha_id = beta_alpha_id;

where alpha table has the form alpha_id, alpha_blah, alpha_blah_blah
and beta table has the form beta_alpha_id, beta_other stuff,
beta_idx, beta_value

This will create 3 columns with totals of beta values according to their idx field

Posted by [name withheld] on Saturday March 29 2003, @3:49am[Delete] [Edit]

when selecting a single random row you have to use a query like this: SELECT ... FROM my_table ORDER BY RAND() LIMIT 1.
as explain shows, mysql optimizes this VERY badly (or may be better said, doens't optimize it at all): it uses an temporary table and an extra filesort.
couldn't this be optimized?!
if not, may be add a syntax like SELECT RANDOM_ROW .... FROM my_table ...

Posted by David Phillips on Wednesday April 2 2003, @11:15am[Delete] [Edit]

This method of selecting a random row should be fast:

LOCK TABLES foo READ;
SELECT FLOOR(RAND() * COUNT(*)) AS rand_row FROM foo;
SELECT * FROM foo LIMIT $rand_row, 1;
UNLOCK TABLES;

Unfortunately, variables cannot be used in the LIMIT clause, otherwise the entire thing could be done completely in SQL.

Posted by [name withheld] on Wednesday August 6 2003, @6:26pm[Delete] [Edit]

If you want to produce a CSV file of the data in a table, the following should accomplish it:

select INTO OUTFILE 'filename' fields terminated by ',' optionally enclosed by '"' escaped by '\\' from tablename;

Remember that the CSV file will be created on the box that mysql is running on. The user that mysqld is running as must have permission to create the file.

If you wish to convert the '\N' in the file which is output to some other string like 'NULL' the following perl script will do that:

#!/usr/bin/perl

while (<>) {
s/\\N/NULL/g;
print;
}

Posted by Josh Rosenbluh on Monday August 11 2003, @6:03am[Delete] [Edit]

Why can't an outfile return the headers? They are far harder to create than to delete.

Add your own comment.