加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MsSql > 正文

非常具有挑战性的SQL访谈(不能使用存储过程)

发布时间:2020-05-31 03:25:40 所属栏目:MsSql 来源:互联网
导读:您有一个SQL表表,其中包含两列:name和pen.两列都是文本字符串. name | pen--------------- mike | redmike | redmike | bluemike | greensteve | redsteve | yellowanton | redanton | blueanton | greenanton | black

您有一个SQL表表,其中包含两列:name和pen.两列都是文本字符串.

name  | pen
---------------    
mike  | red
mike  | red
mike  | blue
mike  | green
steve | red
steve | yellow
anton | red
anton | blue
anton | green
anton | black
alex  | black
alex  | green
alex  | yellow
alex  | red

人名作为输入参数.

请编写一个SQL语句(不是存储过程),它返回具有唯一一组笔的人的名字,这些笔等于或大于/大于给定人员的笔组.

例子:

>输入:迈克
>输出:anton

迈克有(红色,蓝色,绿色).
安东有更多小配件(红色,绿色)黑色.

>输入:史蒂夫
>输出:alex

史蒂夫有(红色,黄色).
亚历克斯有(红色,黄色)绿黑色.
迈克,安东没有打印 – 他们没有黄色.

>输入:alex
>输出:

解决方法

这是一种方式( Online Demo),假设输入名称为“steve”.

这可以改为“寻找没有他们不拥有的史蒂夫钢笔的所有用户”

SELECT DISTINCT name
FROM   table t1
WHERE  NOT EXISTS (SELECT *
                   FROM   table t2
                   WHERE  name = 'steve'
                          AND NOT EXISTS (SELECT *
                                          FROM   table t3
                                          WHERE  t2.pen = t3.pen
                                                 AND t1.name = t3.name))  
AND t1.name <> 'steve' /*Exclude input name from results*/

See this article for other techniques

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读