我的表格'Items'包含内容
id title cat subcat subcat2
1 Notebook 219 Computers Notebooks Ultrabooks
2 Notebook 200 Computers Notebooks Notebooks
3 Notebook 300 Computers Chromebook Notebooks
4 Notebook 233 Computers Notebooks
我正在使用此查询
select cat,subcat,subcat2
from items where cat='Computers'
group by subcat,subcat2
order by subcat asc
此查询返回笔记本电脑,Chromebook,超极本,笔记本电脑。
我希望subcat和subcat2只打印一次。还应删除空白区域
我们可以避免重复的行吗?
答案 0 :(得分:0)
SELECT distinct t1.cat, t1.subcat, t1.subcat2
FROM items as t1
WHERE t1.cat='computers' GROUP BY t1.subcat
输出为
| CAT | SUBCAT | SUBCAT2 |
|-----------|------------|------------|
| Computers | Chromebook | Notebooks |
| Computers | Notebooks | Ultrabooks |
答案 1 :(得分:0)
你在找这样的东西吗?
SELECT subcat
FROM items
WHERE cat = 'computers'
AND COALESCE(subcat, '') <> ''
UNION
SELECT subcat2
FROM items
WHERE cat = 'computers'
AND COALESCE(subcat2, '') <> ''
ORDER BY subcat
输出:
| SUBCAT | |------------| | Chromebook | | Notebooks | | Ultrabooks |
这是 SQLFiddle 演示
答案 2 :(得分:0)
我想你会谈论Notebooks->Notebooks
所以你可以使用以下内容:
select cat,subcat,
CASE WHEN subcat2=subcat THEN '' ELSE subcat2 END
as subcat2
from items where cat='Computers'
group by subcat,
CASE WHEN subcat2=subcat THEN '' ELSE subcat2 END
order by subcat asc