非常具有挑战性的SQL访谈(不能使用存储过程)
发布时间:2020-12-12 06:31:58 所属栏目:MsSql教程 来源:网络整理
导读:您有一个SQL表表,其中包含两列:name和pen.两列都是文本字符串. name | pen--------------- mike | redmike | redmike | bluemike | greensteve | redsteve | yellowanton | redanton | blueanton | greenanton | blackalex | blackalex | greenalex | yellow
您有一个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语句(不是存储过程),它返回具有唯一一组笔的人的名字,这些笔等于或大于/大于给定人员的笔组. 例子: >输入:迈克 迈克有(红色,蓝色,绿色). >输入:史蒂夫 史蒂夫有(红色,黄色). >输入: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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |