db2 sql queries

Upload: amarrai07

Post on 30-May-2018

313 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/14/2019 DB2 SQL Queries

    1/23

    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 ' 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: Each select_expr indicates a column that you want to retrieve. There must be at

    least one select_expr . 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 WHEREclause, 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 WHEREclause.

    In the WHEREclause, you can use any of the functions and operators that MySQLsupports, 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 DUALas a dummy table name in situations where no tables arereferenced:mysql> SELECT 1 + 1 FROM DUAL;

    -> 2

    http://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.html
  • 8/14/2019 DB2 SQL Queries

    2/23

    DUALis purely for the convenience of people who require that all SELECT statements shouldhave FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does notrequire FROM DUALif no tables are referenced.

    In general, clauses used must be given in exactly the order shown in the syntax description.For example, a HAVINGclause must come after any GROUP BYclause and before any

    ORDER BYclause. The exception is that the INTO clause can appear either as shown in thesyntax description or immediately following the select_expr list.

    The list of select_expr terms comprises the select list that indicates which columns toretrieve. Terms specify a column or expression or can use *-shorthand:

    A select list consisting only of a single unqualified * can be used as shorthand toselect 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 ...

    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 examplecould 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 thecomma between two select_expr expressions: MySQL interprets the second asan alias name. For example, in the following statement, columnb is treated as analias 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 WHEREclause, because the columnvalue might not yet be determined when the WHEREclause is executed. SeeSection B.5.5.4, Problems with Column Aliases .

    The FROM table_references clause indicates the table or tables from which toretrieve 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 tablespecified, you can optionally specify an alias.

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.html
  • 8/14/2019 DB2 SQL Queries

    3/23

    tbl_name [[AS] alias ] [ index_hint ]

    The use of index hints provides the optimizer with information about how to chooseindexes during query processing. For a description of the syntax for specifying thesehints, see Section 12.2.8.2, Index Hint Syntax .

    You can use SET max_seeks_for_key= value as an alternative way to forceMySQL to prefer key scans instead of table scans. See Section 5.1.3, Server System Variables .

    You can refer to a table within the default database as tbl_name , or asdb_name .tbl_name to specify a database explicitly. You can refer to a column ascol_name , tbl_name .col_name , or db_name .tbl_name .col_name . You need notspecify a tbl_name or db_name .tbl_name prefix for a column reference unless thereference 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_namealias_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 WHERE t1.name = t2.name; Columns selected for output can be referred to in ORDER BYand GROUP BYclauses

    using column names, column aliases, or column positions. Column positions areintegers 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 thecolumn in the ORDER BYclause that you are sorting by. The default is ascendingorder; this can be specified explicitly using the ASC keyword.

    If ORDER BYoccurs within a subquery and also is applied in the outer query, theoutermost ORDER BYtakes precedence. For example, results for the followingstatement 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 fromthe SQL standard.

    If you use GROUP BY, output rows are sorted according to the GROUP BYcolumns asif you had an ORDER BYfor the same columns. To avoid the overhead of sorting thatGROUP BYproduces, add ORDER BY NULL:

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

    http://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
  • 8/14/2019 DB2 SQL Queries

    4/23

    MySQL extends the GROUP BYclause 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; MySQL extends the use of GROUP BYto allow selecting fields that are not mentioned

    in the GROUP BYclause. If you are not getting the results that you expect from your query, please read the description of GROUP BYfound in Section 11.11, Functionsand Modifiers for Use with GROUP BYClauses .

    GROUP BYallows a WITH ROLLUP modifier. See Section 11.11.2, GROUP BY Modifiers .

    The HAVINGclause is applied nearly last, just before items are sent to the client, withno optimization. ( LIMIT is applied after HAVING.)

    A HAVINGclause can refer to any column or alias named in a select_expr in theSELECT list or in outer subqueries, and to aggregate functions. However, the SQLstandard requires that HAVINGmust reference only columns in the GROUP BYclauseor columns used in aggregate functions. To accommodate both standard SQL and

    the MySQL-specific behavior of being able to refer columns in the SELECT list,MySQL 5.0.2 and up allows HAVINGto refer to columns in the SELECT list, columnsin the GROUP BYclause, columns in outer subqueries, and to aggregate functions.

    For example, the following statement works in MySQL 5.0.2 but produces an error for earlier versions:mysql> SELECT COUNT(*) FROM t GROUP BY col1 HAVING col1 = 2;

    If the HAVINGclause refers to a column that is ambiguous, a warning occurs. In thefollowing statement, col2 is ambiguous because it is used as both an alias and acolumn name:SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;

    Preference is given to standard SQL behavior, so if a HAVINGcolumn name is usedboth in GROUP BYand as an aliased column in the output column list, preference isgiven to the column in the GROUP BYcolumn.

    Do not use HAVINGfor items that should be in the WHEREclause. For example, donot 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 HAVINGclause can refer to aggregate functions, which the WHEREclause

    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. BecauseMySQL also allows GROUP BYand HAVINGto refer to select_expr values, this canresult in an ambiguity:

    http://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.html
  • 8/14/2019 DB2 SQL Queries

    5/23

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

    In that statement, both columns have the name a . To ensure that the correct columnis used for grouping, use different names for each select_expr .

    MySQL resolves unqualified column or alias references in ORDER BYclauses bysearching in the select_expr values, then in the columns of the tables in the FROM clause. For GROUP BYor HAVINGclauses, it searches the FROM clause beforesearching in the select_expr values. (For GROUP BYand HAVING, this differs fromthe 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 theSELECT statement. LIMIT takes one or two numeric arguments, which must both benonnegative 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 theinitial 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 usesome large number for the second parameter. This statement retrieves all rows fromthe 96th row to the last:SELECT * FROM tbl LIMIT 95,18446744073709551615;

    With one argument, the value specifies the number of rows to return from thebeginning of the result set:SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

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

    For prepared statements, you can use placeholders (supported as of MySQL version5.0.7). 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;

    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, theoutermost LIMIT takes precedence. For example, the following statement producestwo rows, not one:(SELECT ... LIMIT 1) LIMIT 2;

    A PROCEDUREclause names a procedure that should process the data in the resultset. For an example, see Section 21.3.1, PROCEDURE ANALYSE , which describes

    ANALYSE, a procedure that can be used to obtain suggestions for optimal columndata types that may help reduce table sizes.

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.html
  • 8/14/2019 DB2 SQL Queries

    6/23

    The SELECT ... INTO OUTFILE ' file_name ' form of SELECT writes the selectedrows 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 amongother things prevents files such as /etc/passwd and database tables from beingdestroyed. As of MySQL 5.0.19, the character_set_filesystem system variable

    controls the interpretation of the file name.The SELECT ... INTO OUTFILE statement is intended primarily to let you veryquickly dump a table to a text file on the server machine. If you want to create theresulting file on some client host other than the server host, you cannot useSELECT ... INTO OUTFILE . In that case, you should instead use a commandsuch as mysql -e "SELECT ..." > file_name to generate the file on the clienthost.

    SELECT ... INTO OUTFILE is the complement of LOAD DATA INFILE . Columnvalues 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 datafile 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 sameFIELDS and LINES clauses that are used with the LOAD DATA INFILE statement.

    FIELDS ESCAPED BY controls how to write special characters. If the FIELDSESCAPED BY character is not empty, it is used as a prefix that precedes followingcharacters on output:

    The FIELDS ESCAPED BY character

    The FIELDS [OPTIONALLY] ENCLOSED BY character

    The first character of the FIELDS TERMINATED BY and LINES TERMINATEDBYvalues

    ASCII NUL (the zero-valued byte; what is actually written following the escapecharacter is ASCII 0, not a zero-valued byte)

    The FIELDS TERMINATED BY , ENCLOSED BY, ESCAPED BY, or LINES TERMINATEDBYcharacters 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 beescaped.

    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 thecharacters 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;

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.html
  • 8/14/2019 DB2 SQL Queries

    7/23

    See Section 12.2.6, LOAD DATA INFILE Syntax , for further information about theFIELDS and LINES clauses, including their default values and allowable values.

    If you use INTO DUMPFILE instead of INTO OUTFILE , MySQL writes only one rowinto the file, without any column or line termination and without performing anyescape 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 userson the server host. The reason for this is that the MySQL server cannotcreate a file that is owned by anyone other than the user under whoseaccount it is running. (You should never run mysqld as root for this andother reasons.) The file thus must be world-writable so that you canmanipulate its contents.

    If the secure_file_priv system variable is set to a nonempty directoryname, 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.8.3.3, SELECT ... INTO Statement ). Theselected values are assigned to the variables. The number of variables must matchthe number of columns. The query should return a single row. If the query returns norows, a warning with error code 1329 occurs ( No data ), and the variable valuesremain unchanged. If the query returns multiple rows, error 1172 occurs ( Resultconsisted of more than one row ). If it is possible that the statement mayretrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.

    The SELECT syntax description at the beginning this section shows the INTO clausenear the end of the statement. It is also possible to use INTO immediately followingthe select_expr list.

    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, rowsexamined 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 toread the examined rows but not to update or delete them. See Section 13.2.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.

    The ALL , DISTINCT , and DISTINCTROW options specify whether duplicate rows should bereturned. If none of these options are given, the default is ALL (all matching rows arereturned). DISTINCT and DISTINCTROW are synonyms and specify removal of duplicaterows from the result set.

    HIGH_PRIORITY , STRAIGHT_JOIN , and options beginning with SQL_ are MySQLextensions to standard SQL.

    http://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.html
  • 8/14/2019 DB2 SQL Queries

    8/23

    HIGH_PRIORITY gives the SELECT higher priority than a statement that updates atable. You should use this only for queries that are very fast and must be done atonce. 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 ( MyISAM , MEMORY,

    MERGE).

    HIGH_PRIORITY cannot be used with SELECT statements that are part of a UNION. 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 joinsthe tables in nonoptimal order. STRAIGHT_JOIN also can be used in thetable_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 optimizationphase of query execution, and references to its columns are replaced with theappropriate column values before query execution proceeds. These tables willappear first in the query plan displayed by EXPLAIN. See Section 7.2.1, OptimizingQueries with EXPLAIN. This exception may not apply to const or system tablesthat are used on the NULL-complemented side of an outer join (that is, the right-sidetable of a LEFT JOIN or the left-side table of a RIGHT JOIN .

    SQL_BIG_RESULT can be used with GROUP BYor DISTINCT to tell the optimizer thatthe result set has many rows. In this case, MySQL directly uses disk-basedtemporary tables if needed, and prefers sorting to using a temporary table with a keyon the GROUP BYelements.

    SQL_BUFFER_RESULTforces the result to be put into a temporary table. This helpsMySQL free the table locks early and helps in cases where it takes a long time tosend the result set to the client. This option can be used only for top-level SELECT

    statements, not for subqueries or following UNION. SQL_SMALL_RESULTcan be used with GROUP BYor DISTINCT to tell the optimizer

    that the result set is small. In this case, MySQL uses fast temporary tables to storethe resulting table instead of using sorting. This should not normally be needed.

    SQL_CALC_FOUND_ROWStells MySQL to calculate how many rows there would be inthe result set, disregarding any LIMIT clause. The number of rows can then beretrieved with SELECT FOUND_ROWS(). See Section 11.10.3, InformationFunctions .

    The SQL_CACHEand SQL_NO_CACHEoptions affect caching of query results in thequery cache (see Section 7.5.5, The MySQL Query Cache ). SQL_CACHEtellsMySQL to store the result in the query cache if it is cacheable and the value of thequery_cache_type system variable is 2 or DEMAND. SQL_NO_CACHEtells MySQLnot to store the result in the query cache. For a query that uses UNION, subqueries,or views, the following rules apply:

    SQL_NO_CACHEapplies if it appears in any SELECT in the query.

    For a cacheable query, SQL_CACHEapplies if it appears in the first SELECT of the query, or in the first SELECT of a view referred to by the query.

    Previous / Next / Up / Table of Contents

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/query-cache.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/replace.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/sql-syntax-data-manipulation.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/query-cache.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/replace.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/sql-syntax-data-manipulation.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index.html
  • 8/14/2019 DB2 SQL Queries

    9/23

    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:

    SELECTSUM(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_valueFROM 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 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 ... FROMmy_table ORDER BY RAND() LIMIT 1.as explain shows, mysql optimizes this VERY badly (or may be better said, doens't optimize it atall): 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 bedone completely in SQL.

    Posted by [name withheld] on August 20 2003 10:55pm [ Delete ] [Edit ]

    In reply to David Philips:

    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.

    http://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881http://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131http://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161http://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235http://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881http://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131http://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161http://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235
  • 8/14/2019 DB2 SQL Queries

    10/23

    Posted by Count Henry De Havilland-Fortesque-Smedley on January 132004 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 HAVINGCOUNT(BookISBN)>1;

    Posted by Count Henry De Havilland-Fortesque-Smedley on January 132004 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 WHEREDealerCatalog.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?

    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 ONDealerCatalog.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 k2WHERE db1.id db2.idAND 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)

    JohannPosted 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 thestring 'wrong'.

    http://dev.mysql.com/doc/mysql/comment.php?id=3850&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3850http://dev.mysql.com/doc/mysql/comment.php?id=3851&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3851http://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985http://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094http://dev.mysql.com/doc/mysql/comment.php?id=3850&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3850http://dev.mysql.com/doc/mysql/comment.php?id=3851&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3851http://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985http://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094
  • 8/14/2019 DB2 SQL Queries

    11/23

    To select those rows for which the 'info' column does *NOT* contain the word 'wrong' one hasto 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:

    $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.:

    http://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121http://ro-escorts.com/http://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142http://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482http://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121http://ro-escorts.com/http://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142http://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482
  • 8/14/2019 DB2 SQL Queries

    12/23

    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 toMySQL). The /*!40000 construct will tell MySQL servers starting from 4.0.0 (which is the firstversion to support SQL_CALC_FOUND_ROWS) to parse the comment, while earlier versionswill 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

    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 ' 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: Each select_expr indicates a column that you want to retrieve. There must be at

    least one select_expr . table_references indicates the table or tables from which to retrieve rows. Its

    syntax is described in Section 12.2.8.1, JOIN Syntax .

    http://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555http://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555http://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/subqueries.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.html
  • 8/14/2019 DB2 SQL Queries

    13/23

    The WHEREclause, if given, indicates the condition or conditions that rows mustsatisfy 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 WHEREclause.

    In the WHEREclause, you can use any of the functions and operators that MySQLsupports, 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 DUALas a dummy table name in situations where no tables arereferenced:mysql> SELECT 1 + 1 FROM DUAL;

    -> 2

    DUALis purely for the convenience of people who require that all SELECT statements shouldhave FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does notrequire FROM DUALif no tables are referenced.

    In general, clauses used must be given in exactly the order shown in the syntax description.For example, a HAVINGclause must come after any GROUP BYclause and before anyORDER BYclause. The exception is that the INTO clause can appear either as shown in thesyntax description or immediately following the select_expr list.

    The list of select_expr terms comprises the select list that indicates which columns toretrieve. Terms specify a column or expression or can use *-shorthand:

    A select list consisting only of a single unqualified * can be used as shorthand toselect 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 ...

    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 examplecould have been written like this:SELECT CONCAT(last_name,', ',first_name) full_name

    http://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.html
  • 8/14/2019 DB2 SQL Queries

    14/23

    FROM mytable ORDER BY full_name;

    However, because the AS is optional, a subtle problem can occur if you forget thecomma between two select_expr expressions: MySQL interprets the second asan alias name. For example, in the following statement, columnb is treated as analias name:

    SELECT columna columnb FROM mytable;For this reason, it is good practice to be in the habit of using AS explicitly whenspecifying column aliases.

    It is not allowable to refer to a column alias in a WHEREclause, because the columnvalue might not yet be determined when the WHEREclause is executed. SeeSection B.5.5.4, Problems with Column Aliases .

    The FROM table_references clause indicates the table or tables from which toretrieve 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 tablespecified, 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 chooseindexes during query processing. For a description of the syntax for specifying thesehints, see Section 12.2.8.2, Index Hint Syntax .

    You can use SET max_seeks_for_key= value as an alternative way to forceMySQL to prefer key scans instead of table scans. See Section 5.1.3, Server System Variables .

    You can refer to a table within the default database as tbl_name , or asdb_name .tbl_name to specify a database explicitly. You can refer to a column ascol_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 thereference 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_namealias_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 WHERE t1.name = t2.name; Columns selected for output can be referred to in ORDER BYand GROUP BYclauses

    using column names, column aliases, or column positions. Column positions areintegers 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;

    http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index-hints.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlhttp://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
  • 8/14/2019 DB2 SQL Queries

    15/23

    SELECT college, region, seed FROM tournament ORDER BY 2, 3;

    To sort in reverse order, add the DESC (descending) keyword to the name of thecolumn in the ORDER BYclause that you are sorting by. The default is ascendingorder; this can be specified explicitly using the ASC keyword.

    If ORDER BYoccurs within a subquery and also is applied in the outer query, theoutermost ORDER BYtakes precedence. For example, results for the followingstatement 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 fromthe SQL standard.

    If you use GROUP BY, output rows are sorted according to the GROUP BYcolumns asif you had an ORDER BYfor the same columns. To avoid the overhead of sorting thatGROUP BYproduces, add ORDER BY NULL:

    SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL; MySQL extends the GROUP BYclause 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; MySQL extends the use of GROUP BYto allow selecting fields that are not mentioned

    in the GROUP BYclause. If you are not getting the results that you expect from your query, please read the description of GROUP BYfound in Section 11.11, Functionsand Modifiers for Use with GROUP BYClauses .

    GROUP BYallows a WITH ROLLUP modifier. See Section 11.11.2, GROUP BY Modifiers .

    The HAVINGclause is applied nearly last, just before items are sent to the client, withno optimization. ( LIMIT is applied after HAVING.)

    A HAVINGclause can refer to any column or alias named in a select_expr in theSELECT list or in outer subqueries, and to aggregate functions. However, the SQLstandard requires that HAVINGmust reference only columns in the GROUP BYclauseor columns used in aggregate functions. To accommodate both standard SQL andthe MySQL-specific behavior of being able to refer columns in the SELECT list,MySQL 5.0.2 and up allows HAVINGto refer to columns in the SELECT list, columnsin the GROUP BYclause, columns in outer subqueries, and to aggregate functions.

    For example, the following statement works in MySQL 5.0.2 but produces an error for earlier versions:

    mysql> SELECT COUNT(*) FROM t GROUP BY col1 HAVING col1 = 2;

    If the HAVINGclause refers to a column that is ambiguous, a warning occurs. In thefollowing statement, col2 is ambiguous because it is used as both an alias and acolumn name:SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;

    http://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.html
  • 8/14/2019 DB2 SQL Queries

    16/23

    Preference is given to standard SQL behavior, so if a HAVINGcolumn name is usedboth in GROUP BYand as an aliased column in the output column list, preference isgiven to the column in the GROUP BYcolumn.

    Do not use HAVINGfor items that should be in the WHEREclause. For example, donot 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 HAVINGclause can refer to aggregate functions, which the WHEREclausecannot:

    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. BecauseMySQL also allows GROUP BYand HAVINGto refer to select_expr values, this canresult in an ambiguity:

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

    In that statement, both columns have the name a . To ensure that the correct columnis used for grouping, use different names for each select_expr .

    MySQL resolves unqualified column or alias references in ORDER BYclauses bysearching in the select_expr values, then in the columns of the tables in the FROM clause. For GROUP BYor HAVINGclauses, it searches the FROM clause beforesearching in the select_expr values. (For GROUP BYand HAVING, this differs fromthe 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 theSELECT statement. LIMIT takes one or two numeric arguments, which must both benonnegative 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 theinitial 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 usesome large number for the second parameter. This statement retrieves all rows fromthe 96th row to the last:SELECT * FROM tbl LIMIT 95,18446744073709551615;

    With one argument, the value specifies the number of rows to return from thebeginning of the result set:SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

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

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.html
  • 8/14/2019 DB2 SQL Queries

    17/23

    For prepared statements, you can use placeholders (supported as of MySQL version5.0.7). 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;

    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, theoutermost LIMIT takes precedence. For example, the following statement producestwo rows, not one:(SELECT ... LIMIT 1) LIMIT 2;

    A PROCEDUREclause names a procedure that should process the data in the resultset. For an example, see Section 21.3.1, PROCEDURE ANALYSE , which describes

    ANALYSE, a procedure that can be used to obtain suggestions for optimal columndata types that may help reduce table sizes.

    The SELECT ... INTO OUTFILE ' file_name ' form of SELECT writes the selectedrows 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 amongother things prevents files such as /etc/passwd and database tables from beingdestroyed. As of MySQL 5.0.19, the character_set_filesystem system variablecontrols the interpretation of the file name.

    The SELECT ... INTO OUTFILE statement is intended primarily to let you veryquickly dump a table to a text file on the server machine. If you want to create theresulting file on some client host other than the server host, you cannot useSELECT ... INTO OUTFILE . In that case, you should instead use a commandsuch as mysql -e "SELECT ..." > file_name to generate the file on the clienthost.

    SELECT ... INTO OUTFILE is the complement of LOAD DATA INFILE . Columnvalues 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 datafile 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.FIELDS ESCAPED BY controls how to write special characters. If the FIELDSESCAPED BY character is not empty, it is used as a prefix that precedes followingcharacters on output:

    The FIELDS ESCAPED BY character

    The FIELDS [OPTIONALLY] ENCLOSED BY character

    http://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.html
  • 8/14/2019 DB2 SQL Queries

    18/23

    The first character of the FIELDS TERMINATED BY and LINES TERMINATEDBYvalues

    ASCII NUL (the zero-valued byte; what is actually written following the escapecharacter is ASCII 0, not a zero-valued byte)

    The FIELDS TERMINATED BY , ENCLOSED BY, ESCAPED BY, or LINES TERMINATEDBYcharacters 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 beescaped.

    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 thecharacters 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;

    See Section 12.2.6, LOAD DATA INFILE Syntax , for further information about theFIELDS and LINES clauses, including their default values and allowable values.

    If you use INTO DUMPFILE instead of INTO OUTFILE , MySQL writes only one rowinto the file, without any column or line termination and without performing anyescape 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 userson the server host. The reason for this is that the MySQL server cannotcreate a file that is owned by anyone other than the user under whoseaccount it is running. (You should never run mysqld as root for this andother reasons.) The file thus must be world-writable so that you canmanipulate its contents.

    If the secure_file_priv system variable is set to a nonempty directoryname, 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.8.3.3, SELECT ... INTO Statement ). Theselected values are assigned to the variables. The number of variables must matchthe number of columns. The query should return a single row. If the query returns norows, a warning with error code 1329 occurs ( No data ), and the variable valuesremain unchanged. If the query returns multiple rows, error 1172 occurs ( Resultconsisted of more than one row ). If it is possible that the statement mayretrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.

    http://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.htmlhttp://dev.mysql.com/doc/refman/5.0/en/load-data.htmlhttp://dev.mysql.com/doc/refman/5.0/en/blob.htmlhttp://dev.mysql.com/doc/refman/5.0/en/mysqld.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html
  • 8/14/2019 DB2 SQL Queries

    19/23

    The SELECT syntax description at the beginning this section shows the INTO clausenear the end of the statement. It is also possible to use INTO immediately followingthe select_expr list.

    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, rowsexamined 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 toread the examined rows but not to update or delete them. See Section 13.2.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.

    The ALL , DISTINCT , and DISTINCTROW options specify whether duplicate rows should bereturned. If none of these options are given, the default is ALL (all matching rows are

    returned). DISTINCT and DISTINCTROW are synonyms and specify removal of duplicaterows from the result set.

    HIGH_PRIORITY , STRAIGHT_JOIN , and options beginning with SQL_ are MySQLextensions to standard SQL.

    HIGH_PRIORITY gives the SELECT higher priority than a statement that updates atable. You should use this only for queries that are very fast and must be done atonce. 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 ( MyISAM , MEMORY,

    MERGE).

    HIGH_PRIORITY cannot be used with SELECT statements that are part of a UNION. 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 joinsthe tables in nonoptimal order. STRAIGHT_JOIN also can be used in thetable_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 optimizationphase of query execution, and references to its columns are replaced with theappropriate column values before query execution proceeds. These tables willappear first in the query plan displayed by EXPLAIN. See Section 7.2.1, OptimizingQueries 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-sidetable of a LEFT JOIN or the left-side table of a RIGHT JOIN . SQL_BIG_RESULT can be used with GROUP BYor DISTINCT to tell the optimizer that

    the result set has many rows. In this case, MySQL directly uses disk-basedtemporary tables if needed, and prefers sorting to using a temporary table with a keyon the GROUP BYelements.

    SQL_BUFFER_RESULTforces the result to be put into a temporary table. This helpsMySQL free the table locks early and helps in cases where it takes a long time to

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.htmlhttp://dev.mysql.com/doc/refman/5.0/en/using-explain.html
  • 8/14/2019 DB2 SQL Queries

    20/23

    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_SMALL_RESULTcan be used with GROUP BYor DISTINCT to tell the optimizer that the result set is small. In this case, MySQL uses fast temporary tables to storethe resulting table instead of using sorting. This should not normally be needed.

    SQL_CALC_FOUND_ROWStells MySQL to calculate how many rows there would be inthe result set, disregarding any LIMIT clause. The number of rows can then beretrieved with SELECT FOUND_ROWS(). See Section 11.10.3, InformationFunctions .

    The SQL_CACHEand SQL_NO_CACHEoptions affect caching of query results in thequery cache (see Section 7.5.5, The MySQL Query Cache ). SQL_CACHEtellsMySQL to store the result in the query cache if it is cacheable and the value of thequery_cache_type system variable is 2 or DEMAND. SQL_NO_CACHEtells MySQLnot to store the result in the query cache. For a query that uses UNION, subqueries,or views, the following rules apply:

    SQL_NO_CACHEapplies if it appears in any SELECT in the query.For a cacheable query, SQL_CACHEapplies if it appears in the first SELECT of the query, or in the first SELECT of a view referred to by the query.

    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:

    SELECTSUM(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_valueFROM alpha JOIN beta WHERE alpha_id = beta_alpha_id;

    where alpha table has the form alpha_id, alpha_blah, alpha_blah_blahand 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 ... FROMmy_table ORDER BY RAND() LIMIT 1.as explain shows, mysql optimizes this VERY badly (or may be better said, doens't optimize it atall): it uses an temporary table and an extra filesort.

    http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/query-cache.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/replace.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/sql-syntax-data-manipulation.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index.htmlhttp://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881http://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131http://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/information-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/query-cache.htmlhttp://dev.mysql.com/doc/refman/5.0/en/union.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/select.htmlhttp://dev.mysql.com/doc/refman/5.0/en/replace.htmlhttp://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://dev.mysql.com/doc/refman/5.0/en/sql-syntax-data-manipulation.htmlhttp://dev.mysql.com/doc/refman/5.0/en/index.htmlhttp://dev.mysql.com/doc/mysql/comment.php?id=1881&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=1881http://dev.mysql.com/doc/mysql/comment.php?id=2131&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2131
  • 8/14/2019 DB2 SQL Queries

    21/23

    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 bedone completely in SQL.

    Posted by [name withheld] on August 20 2003 10:55pm [ Delete ] [Edit ]

    In reply to David Philips:

    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 132004 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 HAVINGCOUNT(BookISBN)>1;

    Posted by Count Henry De Havilland-Fortesque-Smedley on January 132004 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 WHEREDealerCatalog.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?

    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 ONDealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex IS NULL

    http://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161http://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235http://dev.mysql.com/doc/mysql/comment.php?id=3850&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3850http://dev.mysql.com/doc/mysql/comment.php?id=3851&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3851http://dev.mysql.com/doc/mysql/comment.php?id=2161&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=2161http://dev.mysql.com/doc/mysql/comment.php?id=3235&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3235http://dev.mysql.com/doc/mysql/comment.php?id=3850&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3850http://dev.mysql.com/doc/mysql/comment.php?id=3851&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3851
  • 8/14/2019 DB2 SQL Queries

    22/23

    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 k2WHERE db1.id db2.idAND 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 thestring 'wrong'.

    To select those rows for which the 'info' column does *NOT* contain the word 'wrong' one hasto 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:

    $min=1;$row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'table';"));$max=$row["Auto_increment"];

    http://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985http://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094http://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121http://dev.mysql.com/doc/mysql/comment.php?id=3985&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=3985http://dev.mysql.com/doc/mysql/comment.php?id=4094&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4094http://dev.mysql.com/doc/mysql/comment.php?id=4121&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4121
  • 8/14/2019 DB2 SQL Queries

    23/23

    $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.:

    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 toMySQL). The /*!40000 construct will tell MySQL servers starting from 4.0.0 (which is the firstversion to support SQL_CALC_FOUND_ROWS) to parse the comment, while earlier versionswill 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

    http://ro-escorts.com/http://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142http://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482http://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555http://ro-escorts.com/http://dev.mysql.com/doc/mysql/comment.php?id=4142&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4142http://dev.mysql.com/doc/mysql/comment.php?id=4482&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4482http://dev.mysql.com/doc/mysql/comment.php?id=4555&action=deletehttp://dev.mysql.com/doc/mysql/comment.php?id=4555