当我使用子句运行以下查询或指定子查询中的SQL_CALC_FOUND_ROWS
所有颜色时,我得到的记录计数为. 但是当我单独做每种颜色时,我得到的加起来是. 额外的 50 行从何而来?我原以为如果我单独做然后把它们加起来,我会得到,但我得到了。我是否正在查看 IN 子句的工作方式不正确?我更喜欢使用而不是因为我发现要慢得多(+3 秒)RED,BLUE,YELLOW
IN
198
COUNT
248 (72 + 59 + 118)
198
248
COUNT
SQL_CALC_FOUND_ROWS
SQL_CALC_FOUND_ROWS
以下是一些统计数据:
1. I am running MySQL Version: 5.5.2
2. The tables engine is INNODB.
3. All the `CHECK TABLE .... EXTENDED` return OK.
-- Returns 198
SELECT SQL_CALC_FOUND_ROWS DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW','RED','BLUE');
SELECT FOUND_ROWS();
-- Returns 198
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW','RED','BLUE')) all;
-- Returns 72
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('RED')) red;
-- Returns 59
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('BLUE')) blue;
-- Returns 118
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW')) yellow;