如何通过不同的

时间:2015-06-23 22:51:30

标签: mysql

我有一个包含两列的表:col1 | col2。我想选择col2反对空的地方col1和只有一行重复行

我的想法:

// tablename [col1=integer | col2=varchar]
+-------+-------------+
| col1  |    col2     |
+-------+-------------+
|  1    |  anything1  |
|  2    |             |
|  3    |  anything3  |
|  3    |  anything3  |
|  4    |  anything4  |
|  5    |  anything5  |
|  5    |  anything5  |
|  6    |             |
|  7    |  anything7  |
+-------+-------------+

我想要输出

// newtablename
+-------+-------------+
| col1  |    col2     |
+-------+-------------+
|  1    |  anything1  |
|  3    |  anything3  |
|  4    |  anything4  |
|  5    |  anything5  |
|  7    |  anything7  |
+-------+-------------+

我的查询:

select * from tablename where col2 <>'' group by col1

现在我想知道,如何通过distinct (而非group by实现上述查询?

1 个答案:

答案 0 :(得分:0)

你的意思是:

select distinct col1,col2 
from tablename 
where col2 <>'' 
order by col1