0

我有包含内容的表名“项目”

   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 应该只打印一次。也应该删除空白空间

我们有可能避免重复的行吗?

4

3 回答 3

0

引用的 SQLFiddle

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 |
于 2013-11-08T06:43:41.890 回答
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

输出:

| 子猫 |
|------------|
| 浏览器 |
| 笔记本 |
| 超极本 |

这是SQLFiddle演示

于 2013-11-08T06:48:18.237 回答
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

SQLFiddle 演示

于 2013-11-08T06:49:06.703 回答