MySQL basic query Example (2)

Note: This blog query based on a table in a blog post, the blog post: MySQL Query basic example (1) .

1, each of the query table fruits s_id values ​​corresponding to all f_name

<!--以组来进行紧凑-->
mysql> select s_id,group_concat(f_name) as name from fruits group by s_id having count(f_name) > 1;

Returned the following results:

MySQL basic query Example (2)

2, the same statistical value s_id line number?

mysql> select s_id,count(*) as total
    -> from fruits
    -> group by s_id with rollup;

MySQL basic query Example (2)

Note: with effect rollup is s_id after the grouping and then adding the total number of statistics out, which is 16.

3, create a new table and insert data

<!--创建新表-->
mysql> create table orderitems
    -> (
    -> o_num int not null,
    -> o_item int not null,
    -> f_id char(10) not null,
    -> quantity int not null,
    -> item_price decimal(8,2) not null,
    -> primary key(o_num,o_item)
    -> );
<!--插入数据-->
mysql> insert into orderitems(o_num,o_item,f_id,quantity,item_price)
    -> values(30001,1,'a1',10,'5.2'),
    -> (30001,2,'b2',3,'7.6'),
    -> (30001,3,'bs1',5,'11.2'),
    -> (30001,4,'bs2',15,'9.2'),
    -> (30002,1,'b3',2,'20.0'),
    -> (30003,1,'c0',100,10),
    -> (30004,1,'o2',50,'2.50'),
    -> (30005,1,'c0',5,'10'),
    -> (30005,2,'b1',10,'8.99'),
    -> (30005,3,'a2',10,'2.2'),
    -> (30005,4,'m1',5,'14.99');

View the table below:

MySQL basic query Example (2)

4, the same query a column o_num Quantity (number) and ITEM_PRICE (price) multiplication result greater than 100

mysql> select o_num,SUM(quantity*item_price) as total from orderitems
    -> group by o_num having total > 100 order by total;

5, limit the number of rows returned limit--

Limit 1:

<!--只显示表中的前四行-->
mysql> select * from fruits limit 4;

Returned the following results:

MySQL basic query Example (2)

Limit 2:

<!--从第四行开始,显示后面3行-->
mysql> select * from fruits limit 4,3;

Returned the following results:

MySQL basic query Example (2)

6, query each o_num corresponding f_id a few

mysql> select o_num,count(f_id) as items_total 
    -> from orderitems
    -> group by o_num;

Returned results are as follows:
MySQL basic query Example (2)

7, 30005 query o_num as the quantity (number) number

mysql> select sum(quantity) as items_total
    -> from orderitems
    -> where o_num = 30005;

Returned results are as follows:

MySQL basic query Example (2)

8, the average number of queries s_id 103 f_price how much (how much is the average price s_id)

mysql> select avg(f_price) as avg_price from fruitss where s_id = 103;

Returned results are as follows:

MySQL basic query Example (2)

9, query each s_id corresponding average price (f_price) is how much?

mysql> select s_id,avg(f_price) as avg_price from fruits group by s_id;

Returned results are as follows:

MySQL basic query Example (2)

10, each query in f_price maximum value s_id line is which?

mysql> select s_id, max(f_price) as max_price from fruits group by s_id;

Returned results are as follows:

MySQL basic query Example (2)

Similarly, to see the smallest of the line, simply replaced min to max.

11, the maximum value for each query and the corresponding value f_price s_id, f_name.

mysql> select s_id,f_price,f_name from fruits
    -> where f_price in(select max(f_price) from fruits group by s_id);

Returned results are as follows:

MySQL basic query Example (2)

12, re-create the required tables and insert data

<!--创建表-->
mysql> create table suppliers
    -> (
    -> s_id int not null auto_increment,
    -> s_name char(50) not null,
    -> s_city char(50) null,
    -> s_zip char(10) null,
    -> s_call char(50) not null,
    -> primary key(s_id)
    -> );
mysql> create table orders
    -> (
    -> o_num int not null auto_increment,
    -> o_date datetime not null,
    -> c_id int not null,
    -> primary key(o_num)
    -> );
<!--插入数据-->
mysql> insert into suppliers(s_id,s_name,s_city,s_zip,s_call) 
    -> values(101,'FastFruit Inc.','tianjin','300000','48075'),
    -> (102,'LT Supplies','chongqing','400000','44333'),
    -> (103,'acme','shanghai','200000','90046'),
    -> (104,'fnk inc.','zhongshan','528437','11111'),
    -> (105,'good set','taivuang','030000','22222'),
    -> (106,'just eat ours','beijing','010','45678'),
    -> (107,'dk inc.','zhengzhou','450000','33332');
mysql> insert into orders(o_num,o_date,c_id)
    -> values(30001,'2008-09-01',10001),
    -> (30002,'2008-09-12',10003),
    -> (30003,'2008-09-30',10004),
    -> (30004,'2008-10-03',10005),
    -> (30005,'2008-10-08',10001);

13, table join type concepts

During the next query, here it is necessary to say something related to the concept of multi-table queries.

1) coupling

The coupling (inner join) is the most common type of connection, the return lines that match the relationship between the two sets of data, the data row of the coupling together of the overlapping portion within a data set of two mutually intersect.

The coupling between the tables using a comparison operator for comparing operation of certain columns of data, and these tables are listed in the row coupled to match the data.

2) coupling

An outer coupling (outer join) is coupled to the internal extension, except that the duplicate data rows within the coupling portion than the two data sets together, Africa can return the matching data table, or left or right in accordance with the requirements of all data.

An outer coupling may also be divided into the following:

Left outer join (left join or left outer join) the results of all the rows comprises a left table, if the row does not match a row in the right table in the left table, the right table returns NULL, otherwise returns the corresponding value.

Right outer coupling (right join or right outer join) is a left outer join reverse coupling, returns all rows in the right table, if the row does not match a row in the right table in the left table, the left table returns a null value, or returns the corresponding value.

Full coupling (full join or full outer join) returns the left table and the right table for all rows when a row does not match rows in another table, another table returns a null value, otherwise returns the corresponding value.

14, the join query, generates a new two column table designation table

mysql> select suppliers.s_id,s_name,f_name,f_price from fruits inner join suppliers on fruits.s_id = suppliers.s_id;

Returned results are as follows:

MySQL basic query Example (2)

15, left outer join query examples

mysql> select customers.c_id,orders.o_num from customers 
    -> left outer join orders on customers.c_id = orders.c_id
    -> ;

Returned the following results:

MySQL basic query Example (2)

16, other conditions specified in the join query

mysql> select customers.c_id,orders.o_num from customers 
    -> left outer join orders on customers.c_id = orders.c_id
    -> ;

Returned the following results:
MySQL basic query Example (2)

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2456794