mysql - 如何通过distinct实现group by

标签 mysql

我有一个包含两列的表: col1 |列2。我想选择其中 col2 为反对派空和 col1 并且只有一行重复行

我的 table :

// 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)

最佳答案

你的意思是:

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

关于mysql - 如何通过distinct实现group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014805/

相关文章:

mysql - 如何在 Docker 中运行 Sequelize 迁移

MySQL 似乎不能正常工作

php - 日期在数据库中显示为 "1969-12-31"

mysql - 将 MySQL tinyint 类型转换为 bit

php - 无法在 Codeigniter 中更新数据库查询

python - 对于 INSERT,使用带有变量的 LIKE 语句

php - SQL 插入错误 SQL 语法 ... date()

mysql - ERROR 1248 (42000) : Every derived table must have its own alias , 别名存在(带有组和具有的内部查询)

php - 在 CodeIgniter 中选择 AVG

mysql - Zend DB - 刷新临时表