DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(mysql.info) examples

Info Catalog (mysql.info) batch-mode (mysql.info) tutorial (mysql.info) twin
 
 3.6 Examples of Common Queries
 ==============================
 

Menu

 
* example-maximum-column       The Maximum Value for a Column
* example-maximum-row          The Row Holding the Maximum of a Certain Column
* example-maximum-column-group  Maximum of Column per Group
* example-maximum-column-group-row  The Rows Holding the Group-wise Maximum of a Certain Field
* example-user-variables       Using User-Defined Variables
* example-foreign-keys         Using Foreign Keys
* searching-on-two-keys        Searching on Two Keys
* calculating-days             Calculating Visits Per Day
* example-auto-increment       Using `AUTO_INCREMENT'
 
 Here are examples of how to solve some common problems with MySQL.
 
 Some of the examples use the table `shop' to hold the price of each
 article (item number) for certain traders (dealers). Supposing that
 each trader has a single fixed price per article, then (`article',
 `dealer') is a primary key for the records.
 
 Start the command-line tool `mysql' and select a database:
 
      shell> mysql YOUR-DATABASE-NAME
 
 (In most MySQL installations, you can use the database named `test').
 
 You can create and populate the example table with these statements:
 
      mysql> CREATE TABLE shop (
          -> article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
          -> dealer  CHAR(20)                 DEFAULT ''     NOT NULL,
          -> price   DOUBLE(16,2)             DEFAULT '0.00' NOT NULL,
          -> PRIMARY KEY(article, dealer));
      mysql> INSERT INTO shop VALUES
          -> (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
          -> (3,'C',1.69),(3,'D',1.25),(4,'D',19.95);
 
 After issuing the statements, the table should have the following
 contents:
 
      mysql> SELECT * FROM shop;
      +---------+--------+-------+
      | article | dealer | price |
      +---------+--------+-------+
      |    0001 | A      |  3.45 |
      |    0001 | B      |  3.99 |
      |    0002 | A      | 10.99 |
      |    0003 | B      |  1.45 |
      |    0003 | C      |  1.69 |
      |    0003 | D      |  1.25 |
      |    0004 | D      | 19.95 |
      +---------+--------+-------+
 
Info Catalog (mysql.info) batch-mode (mysql.info) tutorial (mysql.info) twin
automatically generated byinfo2html