SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

download SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

of 39

Transcript of SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    1/38

    SELECT Syntax

    [+/-]

    12.2.8.1. JOIN Syntax

    12.2.8.2. Index Hint Syntax

    12.2.8.3. UNION Syntax

    SELECT

    [ALL | DISTINCT | DISTINCTROW ]

    [HIGH_PRIORITY]

    [STRAIGHT_JOIN]

    [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]

    [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]

    select_expr [, select_expr ...]

    [FROM table_references

    [WHERE where_condition]

    [GROUP BY {col_name | expr | position}

    [ASC | DESC], ... [WITH ROLLUP]]

    [HAVING where_condition]

    [ORDER BY {col_name | expr | position}

    [ASC | DESC], ...]

    [LIMIT {[offset,] row_count | row_count OFFSET offset}]

    [PROCEDURE procedure_name(argument_list)]

    [INTO OUTFILE 'file_name'

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    2/38

    [CHARACTER SET charset_name]

    export_options

    | INTO DUMPFILE 'file_name'

    | INTO var_name [, var_name]]

    [FOR UPDATE | LOCK IN SHARE MODE]]

    SELECT is used to retrieve rows selected from one or more tables, and can include UNION statements

    and subqueries. See Section 12.2.8.3, UNION Syntax, and Section 12.2.9, Subquery Syntax.

    The most commonly used clauses of SELECT statements are these:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    3/38

    *mayor

    abram

    fatalaiyaEach select_expr indicates a column that you want to retrieve. There must be at least one

    select_expr.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    4/38

    table_references indicates the table or tables from which to retrieve rows. Its syntax is described in

    Section 12.2.8.1, JOIN Syntax.

    *

    The WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be

    selected. where_condition is an expression that evaluates to true for each row to be selected. The

    statement selects all rows if there is no WHERE clause.

    In the WHERE clause, you can use any of the functions and operators that MySQL supports, except

    for aggregate (summary) functions. See Chapter 11, Functions and Operators.

    SELECT can also be used to retrieve rows computed without reference to any table.

    For example:

    mysql> SELECT 1 + 1;

    -> 2

    You are allowed to specify DUAL as a dummy table name in situations where no tables are referenced:

    mysql> SELECT 1 + 1 FROM DUAL;

    -> 2

    DUAL is purely for the convenience of people who require that all SELECT statements should have FROM

    and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no

    tables are referenced.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    5/38

    In general, clauses used must be given in exactly the order shown in the syntax description. For example,

    a HAVING clause must come after any GROUP BY clause and before any ORDER BY clause. The exception

    is that the INTO clause can appear either as shown in the syntax description or immediately following

    the select_expr list.

    The list of select_expr terms comprises the select list that indicates which columns to retrieve. Terms

    specify a column or expression or can use *-shorthand:

    *

    A select list consisting only of a single unqualified * can be used as shorthand to select all columns

    from all tables:

    SELECT * FROM t1 INNER JOIN t2 ...

    *

    tbl_name.* can be used as a qualified shorthand to select all columns from the named table:

    SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ...

    *

    Use of an unqualified * with other items in the select list may produce a parse error. To avoid this

    problem, use a qualified tbl_name.* reference

    SELECT AVG(score), t1.* FROM t1 ...

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    6/38

    The following list provides additional information about other SELECT clauses:

    *

    A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column

    name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For example:

    SELECT CONCAT(last_name,', ',first_name) AS full_name

    FROM mytable ORDER BY full_name;

    The AS keyword is optional when aliasing a select_expr. The preceding example could have been

    written like this:

    SELECT CONCAT(last_name,', ',first_name) full_name

    FROM mytable ORDER BY full_name;

    However, because the AS is optional, a subtle problem can occur if you forget the comma between

    two select_expr expressions: MySQL interprets the second as an alias name. For example, in the

    following statement, columnb is treated as an alias name:

    SELECT columna columnb FROM mytable;

    For this reason, it is good practice to be in the habit of using AS explicitly when specifying column

    aliases.

    It is not allowable to refer to a column alias in a WHERE clause, because the column value might not

    yet be determined when the WHERE clause is executed. See Section B.5.5.4, Problems with Column

    Aliases.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    7/38

    *

    The FROM table_references clause indicates the table or tables from which to retrieve rows. If you

    name more than one table, you are performing a join. For information on join syntax, see Section

    12.2.8.1, JOIN Syntax. For each table specified, you can optionally specify an alias.

    tbl_name [[AS] alias] [index_hint]

    The use of index hints provides the optimizer with information about how to choose indexes during

    query processing. For a description of the syntax for specifying these hints, see Section 12.2.8.2, Index

    Hint Syntax.

    You can use SET max_seeks_for_key=value as an alternative way to force MySQL to prefer key scans

    instead of table scans. See Section 5.1.4, Server System Variables.

    *

    You can refer to a table within the default database as tbl_name, or as db_name.tbl_name to specify

    a database explicitly. You can refer to a column as col_name, tbl_name.col_name, or

    db_name.tbl_name.col_name. You need not specify a tbl_name or db_name.tbl_name prefix for a

    column reference unless the reference would be ambiguous. See Section 8.2.1, Identifier Qualifiers,

    for examples of ambiguity that require the more explicit column reference forms.

    *

    A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name:

    SELECT t1.name, t2.salary FROM employee AS t1, info AS t2

    WHERE t1.name = t2.name;

    SELECT t1.name, t2.salary FROM employee t1, info t2

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    8/38

    WHERE t1.name = t2.name;

    *

    Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column

    names, column aliases, or column positions. Column positions are integers and begin with 1:

    SELECT college, region, seed FROM tournament

    ORDER BY region, seed;

    SELECT college, region AS r, seed AS s FROM tournament

    ORDER BY r, s;

    SELECT college, region, seed FROM tournament

    ORDER BY 2, 3;

    To sort in reverse order, add the DESC (descending) keyword to the name of the column in the

    ORDER BY clause that you are sorting by. The default is ascending order; this can be specified explicitly

    using the ASC keyword.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    9/38

    If ORDER BY occurs within a subquery and also is applied in the outer query, the outermost ORDER

    BY takes precedence. For example, results for the following statement are sorted in descending order,

    not ascending order:

    (SELECT ... ORDER BY a) ORDER BY a DESC;

    Use of column positions is deprecated because the syntax has been removed from the SQL standard.

    *

    If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an

    ORDER BY for the same columns. To avoid the overhead of sorting that GROUP BY produces, add ORDERBY NULL:

    SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    10/38

    *

    MySQL extends the GROUP BY clause so that you can also specify ASC and DESC after columns

    named in the clause:

    SELECT a, COUNT(b) FROM test_table GROUP BY a DESC;

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    11/38

    *

    MySQL extends the use of GROUP BY to allow selecting fields that are not mentioned in the GROUP

    BY clause. If you are not getting the results that you expect from your query, please read the description

    of GROUP BY found in Section 11.16, Functions and Modifiers for Use with GROUP BY Clauses.

    *

    GROUP BY allows a WITH ROLLUP modifier. See Section 11.16.2, GROUP BY Modifiers.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    12/38

    mayor

    abram

    fatalaiyaThe HAVING clause is applied nearly last, just before items are sent to the client, with no

    optimization. (LIMIT is applied after HAVING.)

    The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or

    columns used in aggregate functions. However, MySQL supports an extension to this behavior, and

    allows HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    13/38

    If the HAVING clause refers to a column that is ambiguous, a warning occurs. In the following

    statement, col2 is ambiguous because it is used as both an alias and a column name:

    SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;

    Preference is given to standard SQL behavior, so if a HAVING column name is used both in GROUP BY

    and as an aliased column in the output column list, preference is given to the column in the GROUP BY

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    14/38

    column.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    15/38

    Do not use HAVING for items that should be in the WHERE clause. For example, do not write the

    following:

    SELECT col_name FROM tbl_name HAVING col_name > 0;

    Write this instead:

    SELECT col_name FROM tbl_name WHERE col_name > 0;

    *

    The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:

    SELECT user, MAX(salary) FROM users

    GROUP BY user HAVING MAX(salary) > 10;

    (This did not work in some older versions of MySQL.)

    *

    MySQL allows duplicate column names. That is, there can be more than one select_expr with the

    same name. This is an extension to standard SQL. Because MySQL also allows GROUP BY and HAVING to

    refer to select_expr values, this can result in an ambiguity:

    SELECT 12 AS a, a FROM t GROUP BY a;

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    16/38

    In that statement, both columns have the name a. To ensure that the correct column is used for

    grouping, use different names for each select_expr.

    *

    MySQL resolves unqualified column or alias references in ORDER BY clauses by searching in the

    select_expr values, then in the columns of the tables in the FROM clause. For GROUP BY or HAVING

    clauses, it searches the FROM clause before searching in the select_expr values. (For GROUP BY and

    HAVING, this differs from the pre-MySQL 5.0 behavior that used the same rules as for ORDER BY.)

    *

    The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.

    LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except

    when using prepared statements).

    With two arguments, the first argument specifies the offset of the first row to return, and the second

    specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

    SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

    To retrieve all rows from a certain offset up to the end of the result set, you can use some large

    number for the second parameter. This statement retrieves all rows from the 96th row to the last:

    SELECT * FROM tbl LIMIT 95,18446744073709551615;

    With one argument, the value specifies the number of rows to return from the beginning of theresult set:

    SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    17/38

    In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

    For prepared statements, you can use placeholders. The following statements will return one row

    from the tbl table:

    SET @a=1;

    PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?';

    EXECUTE STMT USING @a;

    The following statements will return the second to sixth row from the tbl table:

    SET @skip=1; SET @numrows=5;

    PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';

    EXECUTE STMT USING @skip, @numrows;

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    18/38

    For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

    If LIMIT occurs within a subquery and also is applied in the outer query, the outermost LIMIT takes

    precedence. For example, the following statement produces two rows, not one:

    (SELECT ... LIMIT 1) LIMIT 2;

    *

    A PROCEDURE clause names a procedure that should process the data in the result set. For an

    example, see Section 22.4.1, PROCEDURE ANALYSE, which describes ANALYSE, a procedure that can

    be used to obtain suggestions for optimal column data types that may help reduce table sizes.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    19/38

    The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is

    created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be

    an existing file, which among other things prevents files such as /etc/passwd and database tables from

    being destroyed. As of MySQL 5.1.6, the character_set_filesystem system variable controls the

    interpretation of the file name.

    The SELECT ... INTO OUTFILE statement is intended primarily to let you very quickly dump a table to a

    text file on the server machine. If you want to create the resulting file on some client host other than the

    server host, you cannot use SELECT ... INTO OUTFILE. In that case, you should instead use a command

    such as mysql -e "SELECT ..." > file_name to generate the file on the client host.

    SELECT ... INTO OUTFILE is the complement of LOAD DATA INFILE. Column values are written

    converted to the character set specified in the CHARACTER SET clause, which is available as of MySQL5.1.38. Prior to 5.1.38 or if no such clause is present, values are dumped using the binary character set.

    In effect, there is no character set conversion. If a table contains columns in several character sets, the

    output data file will as well and you may not be able to reload the file correctly.

    The syntax for the export_options part of the statement consists of the same FIELDS and LINES

    clauses that are used with the LOAD DATA INFILE statement. See Section 12.2.6, LOAD DATA INFILE

    Syntax, for information about the FIELDS and LINES clauses, including their default values and allowable

    values.

    FIELDS ESCAPED BY controls how to write special characters. If the FIELDS ESCAPED BY character is

    not empty, it is used as a prefix that precedes following characters on output:

    o

    The FIELDS ESCAPED BY character

    o

    The FIELDS [OPTIONALLY] ENCLOSED BY character

    o

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    20/38

    The first character of the FIELDS TERMINATED BY and LINES TERMINATED BY values

    o

    ASCII NUL (the zero-valued byte; what is actually written following the escape character is ASCII

    0, not a zero-valued byte)

    The FIELDS TERMINATED BY, ENCLOSED BY, ESCAPED BY, or LINES TERMINATED BY characters must

    be escaped so that you can read the file back in reliably. ASCII NUL is escaped to make it easier to view

    with some pagers.

    The resulting file does not have to conform to SQL syntax, so nothing else need be escaped.

    If the FIELDS ESCAPED BY character is empty, no characters are escaped and NULL is output as NULL,

    not \N. It is probably not a good idea to specify an empty escape character, particularly if field values in

    your data contain any of the characters in the list just given.

    Here is an example that produces a file in the comma-separated values (CSV) format used by many

    programs:

    SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'

    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

    LINES TERMINATED BY '\n'

    FROM test_table;

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    21/38

    If you use INTO DUMPFILE instead of INTO OUTFILE, MySQL writes only one row into the file, without

    any column or line termination and without performing any escape processing. This is useful if you want

    to store a BLOB value in a file.

    *

    Note

    Any file created by INTO OUTFILE or INTO DUMPFILE is writable by all users on the server host. The

    reason for this is that the MySQL server cannot create a file that is owned by anyone other than the user

    under whose account it is running. (You should never run mysqld as root for this and other reasons.) The

    file thus must be world-writable so that you can manipulate its contents.

    If the secure_file_priv system variable is set to a nonempty directory name, the file to be written

    must be located in that directory.

    *

    The INTO clause can name a list of one or more variables, which can be user-defined variables, or

    parameters or local variables within a stored function or procedure body (see Section 12.7.3.3, SELECT

    ... INTO Statement). The selected values are assigned to the variables. The number of variables must

    match the number of columns. The query should return a single row. If the query returns no rows, a

    warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query

    returns multiple rows, error 1172 occurs (Result consisted of more than one row). If it is possible that

    the statement may retrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.

    In the context of such statements that occur as part of events executed by the Event Scheduler,

    diagnostics messages (not only errors, but also warnings) are written to the error log, and, on Windows,

    to the application event log. For additional information, see Section 19.4.5, Event Scheduler Status.

    *

    The SELECT syntax description at the beginning this section shows the INTO clause near the end of

    the statement. It is also possible to use INTO immediately following the select_expr list.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    22/38

    An INTO clause should not be used in a nested SELECT because such a SELECT must return its result

    to the outer context.

    *

    If you use FOR UPDATE with a storage engine that uses page or row locks, rows examined by the

    query are write-locked until the end of the current transaction. Using LOCK IN SHARE MODE sets a

    shared lock that allows other transactions to read the examined rows but not to update or delete them.

    See Section 13.6.8.3, SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE Locking Reads.

    Following the SELECT keyword, you can use a number of options that affect the operation of the

    statement. HIGH_PRIORITY, STRAIGHT_JOIN, and options beginning with SQL_ are MySQL extensions to

    standard SQL.

    *

    The ALL and DISTINCT options specify whether duplicate rows should be returned. ALL (the default)

    specifies that all matching rows should be returned, including duplicates. DISTINCT specifies removal of

    duplicate rows from the result set. It is an error to specify both options. DISTINCTROW is a synonym forDISTINCT.

    *

    HIGH_PRIORITY gives the SELECT higher priority than a statement that updates a table. You should

    use this only for queries that are very fast and must be done at once. A SELECT HIGH_PRIORITY query

    that is issued while the table is locked for reading runs even if there is an update statement waiting for

    the table to be free. This affects only storage engines that use only table-level locking (such as MyISAM,

    MEMORY, and MERGE).

    HIGH_PRIORITY cannot be used with SELECT statements that are part of a UNION.

    *

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    23/38

    STRAIGHT_JOIN forces the optimizer to join the tables in the order in which they are listed in the

    FROM clause. You can use this to speed up a query if the optimizer joins the tables in nonoptimal order.

    STRAIGHT_JOIN also can be used in the table_references list. See Section 12.2.8.1, JOIN Syntax.

    STRAIGHT_JOIN does not apply to any table that the optimizer treats as a const or system table. Such

    a table produces a single row, is read during the optimization phase of query execution, and references

    to its columns are replaced with the appropriate column values before query execution proceeds. These

    tables will appear first in the query plan displayed by EXPLAIN. See Section 7.2.1, Optimizing Queries

    with EXPLAIN. This exception may not apply to const or system tables that are used on the NULL-

    complemented side of an outer join (that is, the right-side table of a LEFT JOIN or the left-side table of a

    RIGHT JOIN.

    *

    SQL_BIG_RESULT or SQL_SMALL_RESULT can be used with can be used with GROUP BY or DISTINCT

    to tell the optimizer that the result set has many rows or is small, respectively. For SQL_BIG_RESULT,

    MySQL directly uses disk-based temporary tables if needed, and prefers sorting to using a temporary

    table with a key on the GROUP BY elements. For SQL_SMALL_RESULT, MySQL uses fast temporary tables

    to store the resulting table instead of using sorting. This should not normally be needed.

    *

    SQL_BUFFER_RESULT forces the result to be put into a temporary table. This helps MySQL free the

    table locks early and helps in cases where it takes a long time to send the result set to the client. This

    option can be used only for top-level SELECT statements, not for subqueries or following UNION.

    *

    SQL_CALC_FOUND_ROWS tells MySQL to calculate how many rows there would be in the result set,

    disregarding any LIMIT clause. The number of rows can then be retrieved with SELECT FOUND_ROWS().

    See Section 11.14, Information Functions.

    *

    The SQL_CACHE and SQL_NO_CACHE options affect caching of query results in the query cache (see

    Section 7.5.5, The MySQL Query Cache). SQL_CACHE tells MySQL to store the result in the query cache

    if it is cacheable and the value of the query_cache_type system variable is 2 or DEMAND.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    24/38

    SQL_NO_CACHE tells MySQL not to store the result in the query cache. For a query that uses UNION,

    subqueries, or views, the following rules apply:

    o

    SQL_NO_CACHE applies if it appears in any SELECT in the query.

    o

    For a cacheable query, SQL_CACHE applies if it appears in the first SELECT of the query, or in the

    first SELECT of a view referred to by the query.

    Copyright 1997, 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices

    Previous / Next / Up / Table of Contents

    User Comments

    Posted by Colin Nelson on February 26 2003 12: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;

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    25/38

    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 Corin Langosch on March 29 2003 1: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 April 2 2003 7: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 August 20 2003 10:55pm [Delete] [Edit]

    In reply to David Philips:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    26/38

    If your tables are not all that big, a simpler method is:

    SELECT * FROM foo ORDER BY RAND(NOW()) LIMIT 1;

    If it's a big table, your method will almost certainly be faster.

    Posted by Count Henry De Havilland-Fortesque-Smedley on January 13 2004 5:41am [Delete] [Edit]

    If you want to find duplicates on a field that hasn't been uniquely indexed, you can do this:

    SELECT BookISBN, count(BookISBN) FROM Books GROUP BY BookISBN HAVING COUNT(BookISBN)>1;

    Posted by Count Henry De Havilland-Fortesque-Smedley on January 13 2004 5:59am [Delete] [Edit]

    Sometimes you want to retrieve the records that DONT match a select statement.

    Consider this select:

    SELECT CarIndex FROM DealerCatalog, BigCatalog WHERE

    DealerCatalog.CarIndex=BigCatalog.CarIndex

    This finds all the CarIndex values in the Dealer's catalog that are in the bigger distributor catalog.

    How do I then find the dealer CarIndex values that ARE NOT in the bigger catalog?

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    27/38

    The answer is to use LEFT JOIN - anything that doesn't join is given a NULL value , so we look for that:

    SELECT CarIndex FROM DealerCatalog LEFT JOIN BigCatalog ON

    DealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex IS NULL

    Posted by Johann Eckert on February 11 2004 12:14pm [Delete] [Edit]

    To find double entries in a table:

    SELECT db1.*

    FROM tbl_data db1, tbl_data k2

    WHERE db1.id db2.id

    AND db1.name = db2.name

    db1.id must be the PK

    db1.name must be the fields that should be verified as double entries.

    (I'm not sure wether the code is correct but in my case it works)

    Johann

    Posted by [name withheld] on March 2 2004 6:10am [Delete] [Edit]

    In order to anti-match fields by wildcards, one has to check whether the value of the field is not NULL:

    For example: The table 'runs' contains 34876 rows. 205 rows have an 'info' field containing the string

    'wrong'.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    28/38

    To select those rows for which the 'info' column does *NOT* contain the word 'wrong' one has to do:

    mysql> select count(*) FROM runs WHERE info is null or info not like '%wrong%';

    +----------+

    | count(*) |

    +----------+

    | 34671 |

    +----------+

    but not:

    mysql> select count(*) FROM runs WHERE info not like %wrong%';

    +----------+

    | count(*) |

    +----------+

    | 5537 |

    +----------+

    which would lead to a much smaller number of selected rows.

    Posted by M M on March 4 2004 11:28pm [Delete] [Edit]

    I have managed to select random records using php and MySQL like the following:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    29/38

    $min=1;

    $row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'table';"));

    $max=$row["Auto_increment"];

    $random_id=rand($min,$max);

    $row=mysql_fetch_assoc(mysql_query("SELECT * FROM table WHERE id='$random_id'");

    Voila...

    Cezar

    http://RO-Escorts.com

    Posted by Geert van der Ploeg on March 9 2004 5:44am [Delete] [Edit]

    Random records without PHP, only MySQL:

    select * from mailinglists order by rand() limit 1

    Regards,

    Geert van der Ploeg

    Posted by Cody Caughlan on May 26 2004 8:26pm [Delete] [Edit]

    Sometimes it is nice to use the SELECT query options like SQL_CALC_FOUND_ROWS or SQL_CACHE, but

    to maintain compatibility across different databases or even older versions of MySQL which do not

    support those options, it is possible to enclose them in a comment block, e.g.:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    30/38

    SELECT /*! 40000 SQL_CALC_FOUND_ROWS */ foo,bar FROM some_table;

    The /* construct will stop DBMS's other than MySQL from parsing the comment contents, while /*! will

    tell ALL MySQL versions to parse the "comment" (which is actually a non-comment to MySQL). The

    /*!40000 construct will tell MySQL servers starting from 4.0.0 (which is the first version to support

    SQL_CALC_FOUND_ROWS) to parse the comment, while earlier versions will ignore it.

    Posted by Boris Aranovich on June 9 2004 2:33pm [Delete] [Edit]

    I am using this way to select random row or rows:

    SELECT * [or any needed fileds], idx*0+RAND() as rnd_id FROM tablename ORDER BY rnd_id LIMIT 1 [or

    the number of rows]

    Meanwhile, I didn't stumble in any problems with this usage.

    I picked this method in some forum, don't remember when, where or by who was it introduced :)

    Posted by Michal Nedoszytko on August 10 2004 8:19am [Delete] [Edit]

    My method of retrieving duplicate entries

    In a database with personal information (name, surname, etc..) with an auto_increment index I wanted

    to retrieve all the entries with same name and surname field (duplicate names), which by accident were

    inserted to the base.

    I used this syntax

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    31/38

    SELECT name,surname,COUNT(name) AS cnt_n, COUNT(surname) AS cnt_s FROM the_table GROUP BY

    name HAVING cnt_n>1 AND cnt_s>1;

    I hope this might be of help to anyone that wants to do some extended maintenance on the database

    Posted by Dmitri Mikhailov on August 24 2004 7:30pm [Delete] [Edit]

    On the other hand, for this case it's simplier to engage an appropriate index if there is such:

    CREATE INDEX ccr_news_insert_date_i ON ccr_news (insert_date DESC);

    SELECT *

    FROM ccr_news

    WHERE insert_date > 0;

    or, if for some reason MySQL still uses a full table scan:

    SELECT *

    FROM ccr_news FORCE INDEX (ccr_news_insert_date_i)

    WHERE insert_date > 0;

    Posted by Adam Tylmad on August 25 2004 10:36am [Delete] [Edit]

    If you want to ORDER BY [columnname] ASC

    and have the NULL rows in the bottom

    you can use ORDER BY -[columnname] DESC

    Posted by Edward Hermanson on October 6 2004 6:51am [Delete] [Edit]

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    32/38

    Select Name,Category FROM authors ORDER BY Category,Name;

    Will allow you to sort by categories listed in a seperate table

    IF the category column in this primary table contains ID values

    from your ID column in your second reference table.

    So your first "authors" table looks like:

    id name category

    1 Henry Miller 2

    3 June Day 1

    3 Thomas Wolf 2

    and your second reference table looks like:

    id category

    1 Modern

    2 Classics

    Now when the order of categories is changed in the second table

    the order of categories will be reflected in the primary table.

    Then just select the categories from the reference table and put

    the list into a numbered array. Then in your script when you run

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    33/38

    across a category number from the first recordset just reference

    the value from the index in the second array to obtain the value.

    In php in the above example it might look like:

    foreach ($recordset as $key => $record) {

    echo $record["id"] . ":" . $record["name"] . ":" . $ordered_cats[$record["category"]];

    }

    This may seem obvious to some but I was pulling my hair out

    trying to figure out how to order a recordset based on a list

    from a different table. Hope this helps someone.

    Ed

    Posted by Greg Covey on December 2 2004 11:26pm [Delete] [Edit]

    The LIMIT clause can be used when you would use TOP in Access or MS SQL.

    Posted by Kenan Bektas on December 14 2004 5:05pm [Delete] [Edit]

    (LINUX) By default, if you don't specify absolute path for OUTFILE in

    select ... into OUTFILE "..."

    It creates the file in "/var/lib/mysql/"

    Make sure current user has (NOT) a write permission in that directory.

    Posted by Kumar S on January 4 2005 8:27am [Delete] [Edit]

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    34/38

    If You want to find the rows which are having a column with identical values then,

    SELECT managerId, count(company) FROM manager GROUP BY company HAVING COUNT(company)>=8

    (say)

    Regards,

    Kumar.S

    Posted by Imran Chaudhry on February 17 2005 4:13pm [Delete] [Edit]

    I found a nifty way of influencing the ORDER of rows returned by a query that helps in displaying a list

    with frequently accessed items at the top.

    An example is a name/address form where the country is a selectable list. If most of your users are from

    the UK and US you may want to do something like:

    SELECT * FROM countries ORDER by iso_code IN ('UK', 'US') desc

    Which returns something like:

    +----------+----------------------------------------+

    | iso_code | name |

    +----------+----------------------------------------+

    | UK | United Kingdom |

    | US | United States |

    | AF | Afghanistan |

    | AL | Albania |

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    35/38

    | DZ | Algeria |

    | AS | American Samoa |

    Hope this helps someone! [email protected]

    Posted by Fahed Bizzari on February 22 2005 3:18pm [Delete] [Edit]

    It seems there is no way to select * from table where a certain field is distinct. In 2 sqls it is easy:

    $sql9 = "SELECT DISTINCT field AS distinctfield FROM table ORDER BY distinctfield ";

    $res9= $db->execute($sql9);

    for($ll=0;$llgetNumTuples();$ll++)

    {

    $row = $res9->getTupleDirect($ll);

    $distinctfield = $row[distinctfield];

    $sql8="select * from table WHERE field='distinctfield' ORDER BY distinctfield LIMIT 1";

    }

    But not one!

    Fahed

    Posted by Lex Berman on February 22 2005 8:28pm [Delete] [Edit]

    reply to Fahed Bizzari's post, based on Havilland-Fortesque-Smedley's comment (above) the equivalent

    of select * while doing DISTINCT is:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    36/38

    select *, count(FIELD) from TABLE group by FIELD having count(FIELD)=1 into outfile 'foobar.txt';

    then you can check the output. note that there are twice as many rows as records, because each unique

    row is followed by its count (in this case count=1). so just toss the .txt file into something and sort on the

    field containing the count and throw out all the rows =1. this is the same result as a select * distinct

    FIELD (as far as I can tell).

    anyway, works for me. aloha. Lex

    Posted by Lex Berman on February 22 2005 8:32pm [Delete] [Edit]

    oh, about the previous post, it's not correct because distinct should be

    count(FIELD)=>1

    which still doesn't solve the DISTINCT part

    Lex

    Posted by Gregory Turner on March 10 2005 4:00pm [Delete] [Edit]

    In regards to:

    _______________________________________________

    ******************************************

    I found a nifty way of influencing the ORDER of rows returned by a query that helps in displaying a list

    with frequently accessed items at the top.

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    37/38

    An example is a name/address form where the country is a selectable list. If most of your users are from

    the UK and US you may want to do something like:

    SELECT * FROM countries ORDER by iso_code IN ('UK', 'US') desc

    Which returns something like:

    +----------+----------------------------------------+

    | iso_code | name |

    +----------+----------------------------------------+

    | UK | United Kingdom |

    | US | United States |

    | AF | Afghanistan |

    | AL | Albania |

    | DZ | Algeria |

    | AS | American Samoa |

    _______________________________________________

    ******************************************

    If found that if you also add in another 'iso_code' column in the order by statment after the first one

    containing the IN() statment, it will sort the remaining records:

    SELECT * FROM countries ORDER by iso_code IN ('UK', 'US') desc, iso_code

    Posted by Heywood on March 11 2005 2:04pm [Delete] [Edit]

    When using the SELECT ... INTO OUTFILE syntax, use a UNION to add headers. Here's an example for CSV

    output:

  • 8/9/2019 SELECT is Used to Retrieve Rows Selected From One or More Tables, And Can Include UNION Statements

    38/38

    SELECT 'Fiscal Year','Location','Sales'

    UNION

    SELECT FY, Loc, Sales INTO OUTFILE 'salesreport.csv'

    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

    FROM SalesTable;

    This will add the text headers Fiscal Year, Location and Sales to your fields. Only caveat is with an ORDER

    BY statement, if you don't want your headers sorted along with your data you need to enclose it in

    parenthesis:

    SELECT 'Fiscal Year','Location','Sales'

    UNION

    {SELECT FY, Loc, Sales INTO OUTFILE 'salesreport.csv'

    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'