MySQL basic query example

1, create the required tables and insert data

<!--创建表-->
mysql> create table fruits ( f_id char(10) not null, s_id int not null, f_name char(255) not null, f_price decimal(8,2) not null, primary key(f_id) );
<!--插入数据-->
mysql> insert into fruits(f_id,s_id,f_name,f_price)
    -> values('a1',101,'apple','5.2'),
    -> ('b1',101,'blackberry','10.2'),
    -> ('bs1',102,'orange','11.2'),
    -> ('bs2',105,'melon','8.2'),
    -> ('t1',102,'banana','10.3'),
    -> ('t2',102,'grape','5.3'),
    -> ('o2',103,'coconut','9.2'),
    -> ('c0',101,'cherry','3.2'),
    -> ('a2',103,'apricot','2.2'),
    -> ('l2',104,'lemon','6.4'),
    -> ('b2',104,'berry','7.6'),
    -> ('m1',106,'mango','15.7'),
    -> ('m2',105,'xbabay','2.6'),
    -> ('t4',107,'xbababa','2.6'),
    -> ('m3',105,'xxtt','11.6'),
    -> ('b5',107,'xxxx','3.6');
Query OK, 16 rows affected (0.01 sec)
Records: 16  Duplicates: 0  Warnings: 0
<!--创建第二个表-->
mysql> create table customers
    -> (
    -> c_id int not null auto_increment,
    -> c_name char(50) not null,
    -> c_address char(50) null,
    -> c_city char(50) null,
    -> c_zip char(50) null,
    -> c_contact char(50) null,
    -> c_email char(50) null,
    -> primary key(c_id)
    -> );
Query OK, 0 rows affected (0.00 sec)
<!--向第二张表插入数据-->
mysql> insert into customers(c_id,c_name,c_address,c_city,c_zip,c_contact,c_email)
    -> values(10001,'RedHook','200 Street','Tianjin','300000','LiMing','[email protected]'),
    -> (1002,'Stars','333 Fromage Lane','Dalian','116000','Zhangbo','[email protected]'),
    -> (10003,'Netbhood','1 Sunny Place','Qingdao','266000','LuoCong',NULL),
    -> (1004,'JOTO','829 Riverside Drive','Haikou','570000','YangShan','[email protected]');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

2, see the two tables of all data

MySQL basic query example

MySQL basic query example

3, query data table f_name fruits column

mysql> select f_name from fruits;

search result:

MySQL basic query example

4, query fruits and f_price data table f_name two of

mysql> select f_name,f_price from fruits;

Returned the following results:

MySQL basic query example

5, the query table f_name fruits and f_price column, and the value is equal to 5.2 f_price

mysql> select f_name,f_price from fruits where f_price=5.2;

Returned results are as follows:

MySQL basic query example

6, the query table f_name fruits and f_price column, and the value is greater than or equal to 10 f_price

mysql> select f_name,f_price from fruits where f_price >= 10;

Returned results are as follows:

MySQL basic query example

7, the query table f_name fruits and f_price column, and the values ​​in between f_price 2-8

mysql> select f_name,f_price from fruits where f_price between 2 and 8;

Returned results are as follows:

MySQL basic query example

8, the table query fruits f_name s_id and columns, and a value of 101 or 103 s_id

Query Method 1:

mysql> select f_name,s_id from fruits
    -> where s_id = 101 or s_id = 103;

Query Method 2:

mysql> select f_name,s_id from fruits
    -> where s_id in(101,103);

Both query, the returned results are as follows:

MySQL basic query example

9, the query table f_name fruits and s_id column, and the value is not s_id 101 or 103

Query Method 1:

mysql> select f_name,s_id from fruits
    -> where s_id != 101 and s_id != 103;

Query Method 2:

mysql> select f_name,s_id from fruits
    -> where s_id not in(101,103);

Returned results are as follows:

MySQL basic query example

10, Fuzzy query "%" and "_" use

<!--查询fruits表中的f_name列,并且值以“b”开头-->
mysql> select f_name from fruits where f_name like 'b%';

Returned results are as follows:

MySQL basic query example

<!--查询fruits表中的f_name列,并且值以“b”开头,以“y”结尾-->
mysql> select f_name from fruits where f_name like 'b%y';

Returned results are as follows:

MySQL basic query example

<!--查询fruits表中的f_name列,值以“b”开头,以“y”结尾,并且b和y之间有三个字符-->
mysql> select f_name from fruits where f_name like 'b___y';

MySQL basic query example

11, the table query fruits s_id values ​​greater than 101 and the value of row f_price 2.0

mysql> select * from fruits
    -> where s_id = 101 and f_price > '2.0' ;

Returned results are as follows:

MySQL basic query example

12, and the value is 101 or 103 f_price column fruits query table is greater than 5 s_id

mysql> select * from fruits where
    -> s_id in(101,103) and f_price > 5;

MySQL basic query example

13, s_id query table column fruits, and removing duplicates

mysql> select distinct s_id from fruits;

Returned the following results:

MySQL basic query example

14, the query fruits table s_id and f_name columns, and sort the results to s_id

mysql> select s_id,f_name from fruits order by s_id;

Returned results are as follows:

MySQL basic query example

15, the query fruits table f_name and f_price column and the column to sort f_name and f_price

mysql> select f_name,f_price from fruits
    -> order by f_name,f_price;

Returned results are as follows:
MySQL basic query example

Note: multi-field sorting, if the first sort of the same field, the second field will depend on the sort, and so on, if not the same as the first field, directly to the first paragraph to sort.

16, the query f_price column fruits of the table, and the results are sorted in descending order

<!--默认是asc升序排序,可以通过关键字DESC更改为降序-->
mysql> select f_price from fruits order by f_price desc;

MySQL basic query example

17, the query fruits s_id number of different values ​​appearing column, and subjected grouped

<!--调用count(*)函数统计次数,并通过as来对其设置别名,group by来进行分组-->
mysql> select s_id,count(*) as total from fruits group by s_id;

Returned the following results:

MySQL basic query example

18, all values ​​f_name columns in the table for each query fruits s_id corresponding to the same value to f_name line shows, and its value is above a

mysql> select s_id,group_concat(f_name) as name from fruits group by s_id having count(f_name) > 1;

Returned results are as follows:

MySQL basic query example

19, query the customers table rows c_email as null values

mysql> select * from customers where c_email is null;

Query results are as follows:

MySQL basic query example

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

Guess you like

Origin blog.51cto.com/14154700/2456603