sql - 如何在 SQL 中查找表列的唯一组合?
问题描述
我有下表:测试
create table test (Id char)
insert into test values
('A'),
('B'),
('C'),
('D'),
('E')
预期输出:
- a.ID b.ID
- AB
- 交流电
- 广告
- AE
- 公元前
- BD
- 是
- 光盘
- 行政长官
- 德
我尝试了以下代码:
select a.id, b.id from test a cross join test b where a.id<>b.id
但是我当前的输出有无效的组合,上面已经存在参考图像 here(突出显示的记录无效,因为它们以相反的顺序出现在上面)
解决方案
应该相当简单;从切换<>
到<
:
select a.id, b.id from test a cross join test b where a.id < b.id
推荐阅读
- r - 使用 dplyr/tidyverse 在 R 中的 group_by 和 summarise() 多个事物
- javascript - 使用 React Router 对导出的组件进行嵌套路由
- javascript - 单击按钮时显示/隐藏 div 并保存 Cookie
- ember.js - 我希望 Emberjs 中的每条路线都有自己的页面,当我有多个路线时,它们都会在同一页面中显示内容
- javascript - 如何重新加载/刷新 Stripe Checkout 按钮?
- python - Django:如何解决“关键字不能是表达式”错误
- reactjs - 反应原生(expo)加载降价文件
- loops - 在乳胶中,如何循环遍历为每个元素创建关联或宏的数组?
- gstreamer - GStreamer 音频管道在 Linux 上工作,但在 Windows 上不工作
- java - 上传时如何在 Dropbox 中将文件名设置为 FileMetadata