您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關MySQL中怎么實現單表查詢操作,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創建fruits表:
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);
常用查詢:
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";
創建customers表:
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 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;
使用in
操作更加簡潔明了
select f_name,f_price from fruits where s_id in (101 ,102);
字段不重復
SELECT DISTINCT s_id FROM fruits; select f_name from fruits ORDER BY f_name;
如果第一列數據中沒有相同值,將不再對第二列進行排序。
SELECT f_name, f_price FROM fruits ORDER BY f_name, f_price;
按價格降序排列,desc
為降序,默認為升序。
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;
使用having
過濾分組
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id having count(f_name)>1;
在group by
子句中使用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;
創建orderitems表:
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 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;
從第五行開始,讀取3行
SELECT * From fruits LIMIT 4,3;
【例.1】從fruits表中檢索所有字段的數據
SELECT * FROM fruits; SELECT f_id, s_id ,f_name, f_price FROM fruits;
【例.2】查詢當前表中f_name列所有水果名稱,輸入如下語句:
SELECT f_name FROM fruits;
【例.3】例如,從fruits表中獲取f_name和f_price兩列,輸入如下語句:
SELECT f_name, f_price FROM fruits;
【例.4】查詢價格為10.2元的水果的名稱,輸入如下語句:
SELECT f_name, f_price FROM fruits WHERE f_price = 10.2;
【例.5】查找名稱為“apple”的水果的價格,輸入如下語句:
SELECT f_name, f_price FROM fruits WHERE f_name = 'apple';
【例.6】查詢價格小于10的水果的名稱,輸入如下語句:
SELECT f_name, f_price FROM fruits WHERE f_price < 10;
【例.7】s_id為101和102的記錄,輸入如下語句:
SELECT s_id,f_name, f_price FROM fruits WHERE s_id IN (101,102) ORDER BY f_name;
【例.8】查詢所有s_id不等于101也不等于102的記錄,輸入如下語句:
SELECT s_id,f_name, f_price FROM fruits WHERE s_id NOT IN (101,102) ORDER BY f_name;
【例.9】查詢價格在2.00元到10.5元之間水果名稱和價格
SELECT f_name, f_price FROM fruits WHERE f_price BETWEEN 2.00 AND 10.20;
【例.10】查詢價格在2.00元到10.5元之外的水果名稱和價格
SELECT f_name, f_price FROM fruits WHERE f_price NOT BETWEEN 2.00 AND 10.20;
【例.11】查找所有以‘b'字母開頭的水果,輸入如下語句:
SELECT f_id, f_name FROM fruits WHERE f_name LIKE 'b%';
【例.12】在fruits表中,查詢f_name中包含字母‘g'的記錄
SELECT f_id, f_name FROM fruits WHERE f_name LIKE '%g%';
【例.13】查詢以‘b'開頭,并以‘y'結尾的水果的名稱
SELECT f_name FROM fruits WHERE f_name LIKE 'b%y';
【例7.14】在fruits表中,查詢以字母‘y'結尾,且‘y'前面只有4個字母的記錄
SELECT f_id, f_name FROM fruits WHERE f_name LIKE '____y';
【例.15】查詢customers表中c_email為空的記錄的c_id、c_name和c_email字段值:
SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NULL;
【例.16】查詢customers表中c_email不為空的記錄的c_id、c_name和c_email字段值
SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NOT NULL;
【例.17】在fruits表中查詢s_id = ‘101',并且f_price大于5的記錄價格和名稱
SELECT f_id, f_price, f_name FROM fruits WHERE s_id = '101' AND f_price >=5;
【例.18】在fruits表中查詢s_id = ‘101'或者'102',并且f_price大于5,并且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';
【例.19】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name,SQL語句如下:
SELECT s_id,f_name, f_price FROM fruits WHERE s_id = 101 OR s_id = 102;
【例.20】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name
SELECT s_id,f_name, f_price FROM fruits WHERE s_id IN(101,102);
【例.21】查詢fruits表中s_id字段的值,并返回s_id字段值不得重復
SELECT DISTINCT s_id FROM fruits;
【例.22】查詢fruits表的f_name字段值,并對其進行排序
select f_name from fruits ORDER BY f_name;
【例.23】查詢fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序
SELECT f_name, f_price FROM fruits ORDER BY f_name, f_price;
【例.24】查詢fruits表中的f_name和f_price字段,對結果按f_price降序方式排序
SELECT f_name, f_price FROM fruits ORDER BY f_price DESC;
【例.25】查詢fruits表,先按f_price降序排序,再按f_name字段升序排序,SQL語句如下:
SELECT f_price, f_name FROM fruits ORDER BY f_price DESC, f_name;
【例.26】根據s_id對fruits表中的數據進行分組
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;
【例.27】根據s_id對fruits表中的數據進行分組,將每個供應商的水果名稱顯示出來
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id;
【例.28】根據s_id對fruits表中的數據進行分組,并顯示水果種類大于1的分組信息
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id HAVING COUNT(f_name) > 1;
【例.29】根據s_id對fruits表中的數據進行分組,并顯示記錄數量
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;
【例.30】根據s_id和f_name字段對fruits表中的數據進行分組, SQL語句如下,
SELECT * from fruits group by s_id,f_name;
【例.31】查詢訂單價格大于100的訂單號和總訂單價格
SELECT o_num, SUM(quantity * item_price) AS orderTotal FROM orderitems GROUP BY o_num HAVING SUM(quantity*item_price) >= 100;
【例.32】顯示fruits表查詢結果的前4行,輸入如下語句:
SELECT * From fruits LIMIT 4;
【例.33】在fruits 表中,使用LIMIT子句,返回從第5個記錄開始的,行數長度為3的記錄
SELECT * From fruits LIMIT 4, 3;
關于MySQL中怎么實現單表查詢操作就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。