mysql - 使用 DISTINCT 时发生了什么?

标签 mysql sql select distinct

这是我的表格和其中包含的数据:

Table: first

+----------+------+
| first_id | data |
+----------+------+
|        1 |    5 |
|        2 |    6 |
|        3 |    7 |
|        4 |    6 |
|        5 |    7 |
|        6 |    5 |
|        7 |    7 |
|        8 |    6 |
|        9 |    5 |
|       10 |    7 |
+----------+------+

Table: second
+-----------+----------+----------+
| second_id | first_id | third_id |
+-----------+----------+----------+
|         1 |        1 |        2 |
|         2 |        2 |        3 |
|         3 |        3 |        4 |
|         4 |        4 |        2 |
|         5 |        5 |        3 |
|         6 |        6 |        4 |
|         7 |        7 |        2 |
|         8 |        8 |        2 |
|         9 |        9 |        4 |
|        10 |       10 |        4 |
+-----------+----------+----------+

我的目的是获取按 data 字段排序的 third_id 列表。现在,我为此运行了以下查询。

SELECT
    third_id, data
FROM 
    first f JOIN second s ON ( s.first_id = f.first_id )
ORDER BY 
    data ASC;

我得到了预期的结果。

+----------+------+
| third_id | data |
+----------+------+
|        4 |    5 |
|        2 |    5 |
|        4 |    5 |
|        2 |    6 |
|        3 |    6 |
|        2 |    6 |
|        2 |    7 |
|        4 |    7 |
|        4 |    7 |
|        3 |    7 |
+----------+------+

以下查询也按预期工作。

SELECT 
    third_id
FROM 
    first f JOIN second s ON ( s.first_id = f.first_id )
ORDER BY 
    data ASC;

有输出

+----------+
| third_id |
+----------+
|        4 |
|        2 |
|        4 |
|        2 |
|        3 |
|        2 |
|        2 |
|        4 |
|        4 |
|        3 |
+----------+

然后我运行了以下命令。

SELECT DISTINCT
    third_id
FROM 
    first f JOIN second s ON ( s.first_id = f.first_id )
ORDER BY 
    data ASC;

但是,在这里我得到了意想不到的结果:

+----------+
| third_id |
+----------+
|        2 |
|        3 |
|        4 |
+----------+

这里,3 必须在 24 之后,因为我在 data 字段上排序。我究竟做错了什么?还是我必须采取不同的策略。

注意: 这种情况发生在我的项目中。此处提供的表格不属于原始数据库。它是我创建来解释问题的。原始表包含数千行。 如果您想试验数据,我将插入数据库转储:

--
-- Table structure for table `first`
--

CREATE TABLE IF NOT EXISTS `first` (
  `first_id` int(11) NOT NULL AUTO_INCREMENT,
  `data` int(11) NOT NULL,
  PRIMARY KEY (`first_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `first`
--

INSERT INTO `first` (`first_id`, `data`) VALUES
(1, 5),
(2, 6),
(3, 7),
(4, 6),
(5, 7),
(6, 5),
(7, 7),
(8, 6),
(9, 5),
(10, 7);
--
-- Table structure for table `second`
--

CREATE TABLE IF NOT EXISTS `second` (
  `second_id` int(11) NOT NULL AUTO_INCREMENT,
  `first_id` int(11) NOT NULL,
  `third_id` int(11) NOT NULL,
  PRIMARY KEY (`second_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `second`
--

INSERT INTO `second` (`second_id`, `first_id`, `third_id`) VALUES
(1, 1, 2),
(2, 2, 3),
(3, 3, 4),
(4, 4, 2),
(5, 5, 3),
(6, 6, 4),
(7, 7, 2),
(8, 8, 2),
(9, 9, 4),
(10, 10, 4);

最佳答案

你可能想做类似的事情

SELECT third_id
FROM first JOIN second USING (first_id)
GROUP BY third_id
ORDER BY aggregatesomething(data)

min(data)max(data) 或其他。

关于mysql - 使用 DISTINCT 时发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088828/

相关文章:

sql - 如何使用 SQL 执行 JSON 递归嵌套聚合

mysql - 多列主键的 MySQL KEY 值(在 desc 表中)是多少?

java - hibernate 中的 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

mysql - 将Mysql查询的结果导出到excel?

sql - 函数结果的列标题

spring - 如何从 Thymeleaf 表单中选择对象?

mysql子查询在第二张表上找到全名

c# - 无法将属性或索引器 'AnonymousType#1.FirstName' 分配给 -- 它是只读的

javascript - 将 <select> 下拉菜单变成更高级的 JQuery 插件是什么?

mysql - SQL 查询 INNER JOIN 仅当全部存在时