Summary of Common Operation Examples of MySQL Single Table Query
- 2021-10-27 09:32:08
- OfStack
This paper summarizes the common operations of MySQL single table query with examples. Share it for your reference, as follows:
Create an fruits table:
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)
) ;
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.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6);
Common queries:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
Create an customers table:
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(10) NULL,
c_contact char(50) NULL,
c_email char(255) NULL,
PRIMARY KEY (c_id)
);
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', 'LMing@163.com'),
(10002, 'Stars', '333 Fromage Lane', 'Dalian', '116000', 'Zhangbo','Jerry@hotmail.com'),
(10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000', 'LuoCong', NULL),
(10004, 'JOTO', '829 Riverside Drive', 'Haikou', '570000', 'YangShan', 'sam@hotmail.com');
Common query statements:
select c_id,c_name,c_email from customers where c_email is null;
select c_id,c_name,c_email from customers where c_email is not null;
select f_name,f_price from fruits where s_id=101 and f_price>=5;
select f_name,f_price from fruits where s_id in (101,102) and f_price >=5 and f_name="apple";
select f_name,f_price from fruits where s_id=101 or s_id=102;
Use
in
The operation is more concise and clear
select f_name,f_price
from fruits
where s_id in (101 ,102);
Fields do not duplicate
SELECT DISTINCT s_id FROM fruits;
select f_name from fruits ORDER BY f_name;
If the data in Column 1 does not have the same value, Column 2 will no longer be sorted.
SELECT f_name, f_price
FROM fruits
ORDER BY f_name, f_price;
In descending order of price,
desc
Is in descending order, and the default is ascending order.
SELECT f_name, f_price FROM fruits ORDER BY f_price desc;
SELECT f_name, f_price FROM fruits ORDER BY f_price desc,f_name;
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id;
Use
having
Filter packets
SELECT s_id, GROUP_CONCAT(f_name) AS Names
FROM fruits
GROUP BY s_id having count(f_name)>1;
In
group by
Clause is used in the
with rollup
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;
SELECT * from fruits group by s_id,f_name;
Create an orderitems table:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
0
Common query statements:
SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100;
SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100
order by ordertotal;
SELECT * From fruits LIMIT 8;
Start at Line 5 and read 3 lines
SELECT * From fruits LIMIT 4,3;
"Example. 1" Retrieves data for all fields from an fruits table
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
3
[Example. 2] Query all fruit names in the current table column f_name, and enter the following statement:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
4
[Example. 3] For example, get columns f_name and f_price from the fruits table and enter the following statement:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
5
[Example. 4] To query the name of a fruit with a price of 10.2 yuan, enter the following statement:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
6
"Example. 5" To find the price of the fruit named "apple", enter the following statement:
SELECT f_name, f_price
FROM fruits
WHERE f_name = 'apple';
[Example. 6] To query the name of a fruit whose price is less than 10, enter the following statement:
SELECT f_name, f_price
FROM fruits
WHERE f_price < 10;
[Example. 7] s_id for records 101 and 102, enter the following statement:
SELECT * FROM fruits;
select f_name,f_price from fruits;
select f_name,f_price from fruits where f_price=10.2;
select f_name,f_price from fruits where f_price<10;
select * from fruits where s_id in (101,102) order by f_name;
select * from fruits where s_id not in (101,102) order by f_name;
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;
select f_name,f_price from fruits where f_name like "b%";
select f_name,f_price from fruits where f_name like "%g%";
select f_name,f_price from fruits where f_name like "b%y";
select f_name,f_price from fruits where f_name like "____y";
9
"Example. 8" Query all records for which s_id is neither 101 nor 102, and enter the following statement:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id NOT IN (101,102)
ORDER BY f_name;
[Example. 9] Inquire about the name and price of fruits with prices between 2.00 yuan and 10.5 yuan
SELECT f_name, f_price
FROM fruits
WHERE f_price BETWEEN 2.00 AND 10.20;
[Example. 10] Inquire about the names and prices of fruits with prices ranging from 2.00 yuan to 10.5 yuan
SELECT f_name, f_price
FROM fruits
WHERE f_price NOT BETWEEN 2.00 AND 10.20;
[Example. 11] To find all fruits that begin with the letter 'b', enter the following statement:
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE 'b%';
"Example. 12" In the fruits table, query the record in f_name that contains the letter 'g'
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE '%g%';
"Example. 13" Query the name of a fruit that begins with 'b' and ends with 'y'
SELECT f_name
FROM fruits
WHERE f_name LIKE 'b%y';
[Example 7.14] In the fruits table, query records that end with the letter 'y' and are preceded by only four letters' y '
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE '____y';
"Example. 15" Query the c_id, c_name, and c_email field values for records in the customers table where c_email is null:
SELECT c_id, c_name,c_email
FROM customers
WHERE c_email IS NULL;
"Example. 16" Query c_id, c_name, and c_email field values for records in the customers table where c_email is not null
SELECT c_id, c_name,c_email
FROM customers
WHERE c_email IS NOT NULL;
"Example. 17" Query the fruits table for record prices and names for s_id = '101' and f_price greater than 5
SELECT f_id, f_price, f_name
FROM fruits
WHERE s_id = '101' AND f_price >=5;
"Example. 18" Query the fruits table for record prices and names for s_id = '101' or '102' and f_price greater than 5 and f_name= 'apple'
SELECT f_id, f_price, f_name
FROM fruits
WHERE s_id IN('101', '102') AND f_price >= 5 AND f_name = 'apple';
"Example. 19" Query the fruit supplier for f_price and f_name for s_id=101 or s_id=102. The SQL statement is as follows:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id = 101 OR s_id = 102;
[Example. 20] Query f_price and f_name for fruit suppliers with s_id=101 or s_id=102
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id IN(101,102);
"Example. 21" Queries the value of the s_id field in the fruits table and returns that the value of the s_id field must not be duplicated
SELECT DISTINCT s_id FROM fruits;
"Example. 22" Queries and sorts the f_name field values of the fruits table
select f_name from fruits ORDER BY f_name;
[Example. 23] Query the f_name and f_price fields in the fruits table, sorting first by f_name, then by f_price
SELECT f_name, f_price
FROM fruits
ORDER BY f_name, f_price;
"Example. 24" Queries the f_name and f_price fields in the fruits table and sorts the results in descending order f_price
SELECT f_name, f_price
FROM fruits
ORDER BY f_price DESC;
[Example. 25] Query the fruits table, sorting first in descending order of f_price, and then in ascending order of f_name fields. The SQL statement is as follows:
SELECT f_price, f_name
FROM fruits
ORDER BY f_price DESC, f_name;
[Example. 26] Grouping data in an fruits table based on s_id
SELECT s_id, COUNT(*) AS Total
FROM fruits
GROUP BY s_id;
[Example. 27] The data in the fruits table is grouped according to s_id, and the fruit name of each supplier is displayed
SELECT s_id, GROUP_CONCAT(f_name) AS Names
FROM fruits
GROUP BY s_id;
[Example. 28] Groups the data in the fruits table according to s_id, and displays grouping information of fruit types greater than 1
select f_name,f_price
from fruits
where s_id in (101 ,102);
0
[Example. 29] Groups the data in the fruits table according to s_id and displays the number of records
select f_name,f_price
from fruits
where s_id in (101 ,102);
1
"Example. 30" Groups the data in the fruits table according to the s_id and f_name fields. The SQL statement is as follows,
select f_name,f_price
from fruits
where s_id in (101 ,102);
2
[Example. 31] Query the order number and the total order price for which the order price is greater than 100
select f_name,f_price
from fruits
where s_id in (101 ,102);
3
"Example. 32" displays the first four rows of the fruits table query results, and enters the following statement:
select f_name,f_price
from fruits
where s_id in (101 ,102);
4
"Example. 33" In the fruits table, the LIMIT clause is used to return a record with a row length of 3 starting with the fifth record
select f_name,f_price
from fruits
where s_id in (101 ,102);
5
More readers interested in MySQL can check out the topics on this site: "MySQL Common Function Summary", "MySQL Log Operation Skills Collection", "MySQL Transaction Operation Skills Collection", "MySQL Stored Procedure Skills Collection" and "MySQL Database Lock Related Skills Collection"
I hope this article is helpful to everyone's MySQL database.