MySQL单表查询常见操作实例总结

本文实例陈说了MySQL
Union归总查询数据及表别称、字段小名用法。分享给大家供大家参考,具体如下:

本文实例总括了MySQL单表查询左近操作。分享给我们供大家参谋,具体如下:

正文实例总括了MySQL子查询操作。分享给大家供我们参谋,具体如下:

union关键字

创建fruits表:

概念七个表tb1和tb2

SELECT s_id, f_name, f_priceFROM fruitsWHERE f_price  9.0UNION ALLSELECT s_id, f_name, f_priceFROM fruitsWHERE s_id IN(101,103);
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);
CREATE table tbl1 ( num1 INT NOT NULL);CREATE table tbl2 ( num2 INT NOT NULL);

为表取别称

常用查询:

向多少个表中插入数据:

SELECT * from orders AS oWHERE o.o_num = 30001;

SELECT c.c_id, o.o_numFROM customers AS c LEFT OUTER JOIN orders AS oON c.c_id = o.c_id;
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_price10;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";
INSERT INTO tbl1 values(1), (5), (13), (27);INSERT INTO tbl2 values(6), (14), (11), (20);

为字段取别称

创建customers表:

any some关键字的子查询

SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_priceFROM fruits AS f1WHERE f1.f_price  8;

SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')FROM suppliersORDER BY s_name;

SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')as suppliers_titleFROM suppliersORDER BY s_name;
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');
SELECT num1FROM tbl1WHERE num1  ANY (SELECT num2 FROM tbl2);

询问全体价格低于9的鲜果的列表,查询s_id等于101和103怀有的水果和干果品种,使用UNION总是查询结果

常用查询语句:

all一言九鼎字的子查询

SELECT s_id, f_name, f_priceFROM fruitsWHERE f_price  9.0UNION ALLSELECT s_id, f_name, f_priceFROM fruitsWHERE s_id IN(101,103);
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;
SELECT num1FROM tbl1WHERE num1  ALL (SELECT num2 FROM tbl2);

询问全体价格小于9的鲜果的列表,查询s_id等于101和103负有的水果和干果品种,使用UNION ALL老是查询结果,SQL语句如下

使用in操作更为简洁明了

exists根本字的子查询

SELECT s_id, f_name, f_priceFROM fruitsWHERE f_price  9.0UNION ALLSELECT s_id, f_name, f_priceFROM fruitsWHERE s_id IN(101,103);
select f_name,f_pricefrom fruitswhere s_id in (101 ,102);
SELECT * from fruitsWHERE EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);SELECT * from fruitsWHERE f_price10.20 AND EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);SELECT * from fruitsWHERE NOT EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);

为orders表取小名o,查询订30001订单的下单日期

字段不重复

in主要字的子查询

SELECT * from orders AS oWHERE o.o_num = 30001;
SELECT DISTINCT s_id FROM fruits;select f_name from fruits ORDER BY f_name;
SELECT c_idFROM ordersWHERE o_num IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');SELECT c_idFROM ordersWHERE o_num NOT IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');

为customers和orders表分别取别称,并开展三番五回查询

要是第一列数据中绝非相符值,将不再对第二列进行排序。

带相比较运算符的子查询

SELECT c.c_id, o.o_numFROM customers AS c LEFT OUTER JOIN orders AS oON c.c_id = o.c_id;
SELECT f_name, f_priceFROM fruitsORDER BY f_name, f_price;
SELECT s_id, f_name FROM fruitsWHERE s_id =(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

查询fruits表,为f_name取别名fruit_name,f_price取别名fruit_price,为fruits表取别称f1,查询表中f_price
8的水果的名称

按价格降序排列,desc为降序,默以为升序。

所有非

SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_priceFROM fruits AS f1WHERE f1.f_price  8;
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;
SELECT s_id, f_name FROM fruitsWHERE s_id (SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

查询suppliers表中字段s_name和s_city,使用CONCAT函数连接那一个五个字段值,并取列别称叫suppliers_title

使用having过滤分组

概念两个表tb1和tb2

假使未有对连年后的值取外号,其出示列名称将会非常不够直观,输入如下SQL

SELECT s_id, GROUP_CONCAT(f_name) AS NamesFROM fruitsGROUP BY s_id having count(f_name)1;
CREATE table tbl1 ( num1 INT NOT NULL);CREATE table tbl2 ( num2 INT NOT NULL);
SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')FROM suppliersORDER BY s_name;

group by 子句中接纳with rollup

向多个表中插入数据

越来越多关于MySQL相关内容感兴趣的读者可查阅本站专项论题:《MySQL常用函数大汇总》、《MySQL日志操作才能大全》、《MySQL事务操作技艺汇总》、《MySQL存储进程手艺大全》及《MySQL数据库锁相关能力汇总》

SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;SELECT * from fruits group by s_id,f_name;
INSERT INTO tbl1 values(1), (5), (13), (27);INSERT INTO tbl2 values(6), (14), (11), (20);

可望本文所述对我们MySQL数据库计有所援救。

创建orderitems表:

回来tbl2表的全部 num2 列,然后将 tbl1 中的 num1
的值与之实行相比,只要超越 num2的别的值为适合查询条件的结果

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)) ;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);
SELECT num1FROM tbl1WHERE num1  ANY (SELECT num2 FROM tbl2);

常用查询语句:

回到tbl1表的中比tbl2表num2 列全部值都大的值

SELECT o_num, SUM(quantity * item_price) AS orderTotalFROM orderitemsGROUP BY o_numHAVING SUM(quantity*item_price) = 100;SELECT o_num, SUM(quantity * item_price) AS orderTotalFROM orderitemsGROUP BY o_numHAVING SUM(quantity*item_price) = 100order by ordertotal;SELECT * From fruits LIMIT 8;
SELECT num1FROM tbl1WHERE num1  ALL (SELECT num2 FROM tbl2);

从第五行最先,读取3行

查询表suppliers表中是或不是存在s_id=107的经销商,假设存在则查询fruits表中的记录

SELECT * From fruits LIMIT 4,3;
SELECT * from fruitsWHERE EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);

从fruits表中寻觅全体字段的数量

查询表suppliers表中是或不是存在s_id=107的代理商,假设存在则查询fruits表中的f_price大于10.20的记录

SELECT * FROM fruits;SELECT f_id, s_id ,f_name, f_price FROM fruits;
SELECT * from fruitsWHERE f_price10.20 AND EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);

查询当前表中f_name列全体水果名称,输入如下语句:

询问表suppliers表中是不是存在s_id=107的承包商,假使荒诞不经则查询fruits表中的记录

SELECT f_name FROM fruits;
SELECT * from fruitsWHERE NOT EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);

举个例子,从fruits表中收获f_name和f_price两列,输入如下语句:

在orderitems表中询问订购f_id为c0的订单号,并依赖订单号查询所有订单号的客商c_id