【发布时间】:2019-05-07 20:38:03
【问题描述】:
我在 centos 6 机器上使用 MySQL 5.6(mysql Ver 14.14 Distrib 5.6.44,用于 Linux (i686),使用 EditLine 包装器)
我有一个包含多个内部联接的查询。我已经确定,当使用带有 SQL_CALC_FOUND_ROWS 的 ORDER BY 子句时,不会返回任何数据。查询对消息说“ok”,我有一个查询花费了多长时间的持续时间 - 但没有返回任何内容。
如果我删除“SQL_CALC_FOUND_ROWS”然后我得到我的行。 在 MySQL 工作台中,我可以看到持续时间,但在“获取”时间下,它甚至不会尝试获取。
查询是:
SELECT SQL_CALC_FOUND_ROWS
l.id AS licensee_id,
l.agency_id,
l.licensee_fname,
l.licensee_name,
l.licensee_lname,
l.licensee_email,
licensee_certs.cert_number,
agency.agency_name,
licensee_cert_types.cert_name,
licensee_certs.cert_issue_date AS approval_timestamp,
licensee_certs.cert_issue_date AS issue_timestamp,
licensee_certs.cert_expiration_date AS expire_timestamp,
licensee_certs.cert_status AS licensee_status,
licensee_certs.cert_status,
(licensee_certs.cert_expiration_date - UNIX_TIMESTAMP()) AS days_remaining,
h_cache.acquired AS total_hours,
h_cache.pending AS pending_hours,
CONCAT(IFNULL(licensee_cert_types.cert_name, ''),
' ',
IFNULL(licensee_certs.cert_number, ''),
' ',
IFNULL(licensee_certs.cert_status, ''),
' ',
IFNULL(l.licensee_fname, ''),
' ',
IFNULL(l.licensee_lname, ''),
' ',
IFNULL(l.licensee_email, ''),
' ',
IFNULL(agency.agency_name, ''),
' ',
IFNULL(agency.agency_abbr, '')) AS search
FROM
licensee AS l
LEFT JOIN
licensee_certs ON l.id = licensee_certs.licensee_id
AND licensee_certs.agency_id = l.agency_id
LEFT JOIN
agency ON agency.id = l.agency_id
LEFT JOIN
licensee_cert_hour_cache AS h_cache ON h_cache.licensee_id = l.id
AND h_cache.agency_id = l.agency_id
AND h_cache.licensee_cert_type_id = licensee_certs.cert_type
INNER JOIN
licensee_cert_types ON licensee_certs.cert_type = licensee_cert_types.id
GROUP BY licensee_certs.id
ORDER BY l.licensee_fname ASC
limit 5
如果我删除订单,那么我会得到我的 5 行。 或者,如果我删除“SQL_CALC_FOUND_ROWS”,我会得到 5 行。
为什么我不能同时进行 order by 和 SQL calc?
我认为同样重要的是要注意这个确切的查询在 MySQL 5.5 上运行良好。我升级到 5.6.44 并出现此行为。
在我们的生产服务器上,运行 5.6.42 - 我们对此查询没有任何问题。
更新: 似乎是上面提到的这个特定查询。 如果我使用 SQL_CALC + ORDER BY 运行不同的查询,我会得到结果,即:
SELECT SQL_CALC_FOUND_ROWS
p.*,
a.agency_name,
(SELECT
COUNT(*)
FROM
providership_notes
WHERE
providership_notes.provider_id = p.id) AS note_count,
(SELECT
`user_id`
FROM
`user_entity_relations`
WHERE
`instance_id` = p.id
AND `entity_type_id` = 2
LIMIT 1) AS `user_id`,
CONCAT(IFNULL(providership_name, ''),
' ',
IFNULL(providership_abbr, ''),
' ',
IFNULL(agency_name, ''),
' ',
IFNULL(providership_state, '')) AS search
FROM
providership AS p
LEFT JOIN
agency_providership_relations AS r ON r.providership_id = p.id
AND `r`.`status` = 'approved'
LEFT JOIN
agency AS a ON r.agency_id = a.id
GROUP BY p.id
ORDER BY providership_name ASC
LIMIT 0 , 10```
【问题讨论】:
-
听起来像一个错误。这是一个有点类似的问题:stackoverflow.com/questions/42869881/…
标签: mysql sql-order-by sql-calc-found-rows