Common query statements for the MySQL Getting Started tutorial

Source: Internet
Author: User


Sample database:

CREATE TABLE Shop (
Article INT (4) UNSIGNED zerofill DEFAULT ' 0000 ' not NULL,
Dealer CHAR DEFAULT ' not NULL,
Price DOUBLE (16,2) DEFAULT ' 0.00 ' is not NULL,
PRIMARY KEY (article, dealer));

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

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 |
+---------+--------+-------+


1. Column Maximum value

What's the biggest item number?

SELECT MAX (article) from shop;

2. The row that owns the maximum value of a column

Find out the numbers, sellers and prices of the most expensive items?

Select article, Dealer price    from   shop    where  price= (select MAX (price) from the shop); Another solution is to sort all the rows in descending order and use the MySQL-specific limit clause to get to the first line:

SELECT article, dealer, price    from shop    order by Price desc    LIMIT 1;3. Column maximum: By group what is the maximum price for each item? SELECT article, MAX (price) as price
    from   Shop
    GROUP by article< br> +---------+-------+
| article | price |
+---------+-------+
|    0001 |  3.99 |
|& nbsp;   0002 | 10.99 |
|    0003 |  1.69 |
|    0004 | 19.95 |
+---------+-------+
4. Row with the maximum number of groups in a field for each item, find the most expensive items of the dealer? SELECT article, dealer, price
    from   shop S1
    where  price= ( SELECT MAX (s2.price)
              from shop S2
              WHERE s1.article = s2.article );
5. Use User variables to find the items with the highest or lowest price?

Mysql> SELECT @min_price: =min (Price), @max_price: =max (price) from shop;
Mysql> SELECT * from shop WHERE price= @min_price OR price= @max_price;
+---------+--------+-------+
| Article | dealer | Price |
+---------+--------+-------+
| 0003 |  D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+
6. Search for two keywords that are grouped together by or by using two key searches:

SELECT Field1_index, field2_index from test_table WHERE field1_index = ' 1 ' OR field2_index = ' 1 '

You can also use union to synthesize the output of two separate SELECT statements together:

SELECT Field1_index, field2_index from test_table WHERE field1_index = ' 1 ' unionselect field1_index, Field2_index FRO M test_table WHERE field2_index = ' 1 ';

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.