
sql
May_chen123
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
SQL窗口函数轻松解决连续N天购买用户问题
一、了解数据 订单表表名:ord 字段:name1:客户编号,orderdate:订购日期,amount:购买数量 1、找出有连续三天购物的用户 2、计算每位客户相邻两次购物的时间差 1、 找出有连续三天购物的用户 方法一: 连续三天购物,就是同一个用户相邻的订单日期都相差1天,比如A001用户2019/10/4、2019/10/5、2019/10/6就是连续三天购物。 可以通过日期将表自连接起来,如果以客户编号相同和日期隔一天为连接条件将表自连接,存在隔一天的订单才能连接,不存在即不能连接,就能找到连原创 2020-08-04 20:58:38 · 6203 阅读 · 1 评论 -
MySQL -concat文本连接函数怎么用
文本连接函数: concat ()、concat_ws ()和 group_concat () 参数介绍: concat(str1,str2,…)str:需要拼接的文本列或文本,数目不限制 concat_ws(separator,str1,str2,…) 参数①separater:分隔符("-"、""、"+"等) 参数②str:要拼接的值(文本/文本列); group_concat(expr) expr代表运算表达式: 参数①str:要拼接的值(文本/文本列); 参数②order by:排序(表里任一字段)原创 2020-07-29 17:51:01 · 675 阅读 · 0 评论 -
MySQL count(if)和 sum(if)的用法
有这样一个分类表包括ID、分类及时间信息。 因为需要将分类变成新列,首先想到的是sum(if)函数,仔细观察后发是通过时间来列来计数,看不同时间不同分类的个数。 sum(if(fenlei=‘分类1’,1,0))fenlei_1 意思:如果是’分类1’的则返回1,否则返回0,只有是分类1的才求和。 SELECT id, sum(if(fenlei='分类1',1,0))fenlei_1, sum(if(fenlei='分类2',1,0))fenlei_2, sum(if(fenlei='分类3',1,0)原创 2020-07-02 20:22:45 · 1149 阅读 · 0 评论 -
SQLZOO selfjoin/zh——爱丁堡巴士
1.數據庫中有多少個站stops。 select count(name) from stops; 2.找出車站 ‘Craiglockhart’ 的 id select id from stops where name= 'Craiglockhart'; 3.列出巴士公司’LRT’的’4’號巴士線的站編號id 和 站名name select id,name from stops join route on id=stop where num='4' and company='LRT'; Routes .原创 2020-06-28 17:27:03 · 3732 阅读 · 1 评论 -
sqlzoo——nss数据集
Contents 1 Check out one row 2 Calculate how many agree or strongly agree 3 Unhappy Computer Students 4 More Computing or Creative Students? 5 Strongly Agree Numbers 6 Strongly Agree, Percentage 7 Scores for Institutions in Manchester 8 Number of Computin.原创 2020-06-28 10:39:09 · 583 阅读 · 0 评论 -
SQLZOO——苏格兰议会数据
1.一個成員被工黨逐出黨,現沒屬任何黨。找出他。 select name from msp where party is null; 2.列出每個黨及其領導人。 select name,Leader from party group by name 3.列出每個黨及其領導人,這些黨其實是沒有領導人的。 SELECT NAME,leader FROM party WHERE LEADER IS not NULL; 4.列出政黨名單,當中最少有一名黨員在議會內。 select party.name f.原创 2020-06-26 23:27:09 · 268 阅读 · 0 评论 -
SQL:Using null/join/case/coalesce—老师数据
NULL, INNER JOIN, LEFT JOIN, RIGHT JOIN 1.列出學系department是NULL值的老師。 為何不能用 = 你可能會以為 dept=NULL 是可行的,但它不是。你要使用dept IS NULL select name from teacher where dept is null 2.注意INNER JOIN 不理會沒有學系的老師及沒有老師的學系 SELECT teacher.name, dept.name FROM teacher INNER JOIN d.原创 2020-06-26 17:12:12 · 255 阅读 · 0 评论 -
sqlzoo 表的拼接练习——电影数据库
sqlzoo more join——电影数据库 1.列出1962年首影的電影, [顯示 id, title] SELECT id,title FROM movie WHERE yr=1962 2.電影大國民 ‘Citizen Kane’ 的首影年份。 select yr from movie where title= 'Citizen Kane' 3.列出全部Star Trek星空奇遇記系列的電影,包括id, title 和 yr(此系統電影都以Star Trek為電影名稱的開首)。按年份順序排原创 2020-06-26 13:16:49 · 799 阅读 · 0 评论