SQL不仅可以从数据库中读取数据,还能通过不同的SQL函数语句直接返回所需要的结果,从而大大提高了自己在客户端应用程序中计算的效率。
查找重复记录
Where (nick_name,password) in
(
SELECT nick_name,password
FROM user
group by nick_name,password
having count(nick_name)>1
);
WHERE id in
(SELECT max(id) FROM user
group by nick_name,password
having count(nick_name)>1
);
FROM customer c1,customer c2
WHERE c1.cust_email=c2.cust_email
AND c1.id>c2.id;
(SELECT nick_name,password FROM
(SELECT nick_name,password FROM user
group by nick_name,password
having count(nick_name)>1) as tmp1
)
and id not in
(SELECT id FROM
(SELECT min(id) id FROM user
group by nick_name,password
having count(nick_name)>1) as tmp2
);
查找缺失值记录
WHERE cust_email IS NULL;
WHERE city IS NULL;
UPDATE orderitems set
price_new=IFNULL(price_new,5.74);
SELECT IFNULL(price_new,5.74) AS bus_ifnull
FROM orderitems;
更新表添加计算列
UPDATE orderitems set price_new= item_price*count;
多列排序
ORDER BY price_new DESC,quantity;
ORDER BY price_new DESC Limit 5;
FROM orderitems
ORDER BY price_new DESC LIMIT 9,1;
(SELECT COUNT(DISTINCT prod_price)
FROM products
WHERE prod_price>=a.prod_price
) AS rank
FROM products AS a
ORDER BY rank ;
字符串替换
SELECT city FROM data1;
MySQL 字符串截取函数:left(), right(), substring(), substring_index()
通过操作符实现高级筛选
WHERE vend_id IN('DLL01','BRS01');
SELECT prod_name FROM Products WHERE NOT vend_id='DLL01';
FROM Vendors INNER JOIN Products
ON Vendors.vend_id=Products.vend_id;
SELECT prod_name,vend_name,prod_price,quantity
FROM OderItems,Products,Vendors
WHERE Products.vend_id=Vendors.vend_id
AND OrderItems.prod_id=Products.prod_id
AND order_num=20007;
FROM Customers as c1,Customers as c2
WHERE c1.cust_name=c2.cust_name
AND c2.cust_contact='Jim Jones';
数据分组
FROM OrderItems
GROUP BY order_num HAVING COUNT(*)>=3;
CASE WHEN colour = "A" THEN price END AS A,
CASE WHEN colour = "B" THEN price END AS B,
CASE WHEN colour = "C" THEN price END AS C,
CASE WHEN colour = "F" THEN price END AS F
FROM data1
推荐阅读
🧐分享、点赞、在看,给个三连击呗!👇