简体   繁体   中英

SQL Query - Select Distinct COUNT of non-primary ID column

I am trying to count how many individual IDs I have in the LogID column of my table, which is something I should easily be able to do via

$sql = ("SELECT COUNT(distinct LogID) FROM qci_dmlog_data");

LogID is NOT my primary / unique ID column for this table, instead its a reference number which can appear more than once throughout the table. Fact is, I have 9 rows with 8 UNIQUE LogID's (the first 2 are the same) but the above code always spits out 9 as a result.

在此处输入图片说明

What am I doing wrong here? Thanks

The reason your code would spit out 9 is because two columns that look the same are not. In this case, the suspect values are the rows with 446289.

I imagine that the values are not integers, because with integers "what you see is what you get". Depending on the data type, one of two things is likely.

If the data type is a floating point representation, then one of the values might be something like 446288.999997. The output might just be rounded to the nearest integer (for some reason).

If the data type is a string, then there are hidden characters of some sort. This would generally fall into one of three categories:

  • Spaces at the end of the string (or less likely at the beginning)
  • Characters that look like numbers but are not (think "O" the letter versus "0" the number). This gets much worse when you expand the character set.
  • Unprintable characters.

Try out:

SELECT LogId, Count(LogId) AS count 
FROM qci_dmlog_data 
HAVING Count(LogId) = 1
GROUP BY LogId;

Maybe this will work better for you

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