简体   繁体   中英

SELECT COUNT(DISTINCT `Table`.`ID`) returns 0 for only specific tables

While using the rails_admin app for managing our database, we noticed that our pagination at the bottom for two of our tables, 'Signals' and 'SignalsHistory' was missing and it just said there was 0 of them, despite being able to see records listed in the view.

Further investigation led to me checking the development logs and seeing what query rails was executing to get the row count for pagination.

SELECT COUNT(DISTINCT `SignalsHistory`.`ID`) FROM `SignalsHistory` LEFT OUTER JOIN `Signals` ON `Signals`.`ID` = `SignalsHistory`.`SignalsID`

This returns

+---------------------------------------+
| COUNT(DISTINCT `SignalsHistory`.`ID`) |
+---------------------------------------+
|                                     0 |
+---------------------------------------+

I know that there are actually 16196 unique instances of the ID field within my table, because it's the primary key. Here's the table for comparison:

mysql> describe SignalsHistory;
+----------------+-------------+------+-----+----------------------+----------------+
| Field          | Type        | Null | Key | Default              | Extra          |
+----------------+-------------+------+-----+----------------------+----------------+
| ID             | int(11)     | NO   | PRI | NULL                 | auto_increment |
| SignalsID      | int(11)     | NO   | MUL | NULL                 |                |
| CreateDateTime | datetime(6) | YES  |     | CURRENT_TIMESTAMP(6) |                |
| Register       | int(11)     | YES  |     | NULL                 |                |
| RecordNo       | int(11)     | YES  |     | NULL                 |                |
| Invalid        | tinyint(1)  | YES  |     | NULL                 |                |
+----------------+-------------+------+-----+----------------------+----------------+
6 rows in set (0.00 sec)

This same query returns the right number of results when used on different tables, (like Devices . ID , Sites . ID , etc.) but not on this table in particular.

To confirm I could in fact count them without the Distinct clause I ran it again without it:

mysql> SELECT COUNT(`SignalsHistory`.`ID`) FROM `SignalsHistory` LEFT OUTER JOIN `Signals` ON `Signals`.`ID` = `SignalsHistory`.`SignalsID`;
+------------------------------+
| COUNT(`SignalsHistory`.`ID`) |
+------------------------------+
|                        16196 |
+------------------------------+
1 row in set (0.01 sec)

I looked online as to why the DISTINCT could be breaking my count query, and I found this post which I then tried to mimic to see if I could get it to work, like so:

mysql> SELECT COUNT(*) FROM (SELECT DISTINCT ID FROM SignalsHistory WHERE ID IS NOT NULL) test;
+----------+
| COUNT(*) |
+----------+
|    16196 |
+----------+
1 row in set (0.00 sec)

Still not working, I turn to you all. I'm pulling my hair out over this and don't really know what's going on since it seems to be limited to this table.

I know that Signal is a reserved word in MySQL as of late and that's why I'm avoiding using it explicitly. Could 'SignalsHistory' be messing with that?

My MySQL version is:

mysql --version
mysql  Ver 14.14 Distrib 5.6.33, for debian-linux-gnu (x86_64) using  EditLine wrapper

正如Barmar评论的那样,这是 MySQL 中一个已知且长期存在的错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM