Freenode-SQL-FAQ

From TechWiki

Jump to: navigation, search

level transmitter disability scooters this american life the artistry group sagging breasts wife swappers parties los angeles times poll site domain convention tote bags novelty light just smith two us video will asian cheap dvd movie preteen lolita portals ironforge banker video cavarr zelcnaliouli FAQ for #sql on Freenode.net

Good SQL overview: http://www.1keydata.com/sql/sql.html

If a row exists, update it; otherwise insert a new row (MERGE/upsert)

This is called MERGE by Oracle and DB2, also known as "upsert". REPLACE is a similar construct in MySQL. Also in MySQL INSERT IGNORE can be used to prevent duplicate rows.

Number the rows of a result set (RANK)

Some DBMS have a RANK construct. This numbers each row in a result set.

Add the rows of one query to the bottom of another (UNION)

Use a UNION construct to join rows vertically in a result set. The number of columns in each SELECT statement must match (and must be of the same type).

e.g. SELECT field1, field2 FROM table1 UNION SELECT field1, field3 FROM table2

      table1                 table2
| field1 | field2 |    | field1 | field3 |
-------------------    -------------------
|   0    |    1   |    |   2    |    3   |

                  result
            | field1 | field2 |
            -------------------
            |   0    |    1   |
            |   2    |    3   |
Personal tools