简体   繁体   English

MySQL:如何仅使用一个查询获取表中不存在的ID的百分比

[英]MySQL: How to get the percentage of non existing IDs in a table using only one query

Actually I am using two queries to get 其实我正在使用两个查询来获取

  1. the sum of existing IDs in a table 表中现有ID的总和
  2. the missing IDs in the same table 同一张表中缺少的ID

then I use PHP to calculate the percentage of the missing IDs in this table. 然后使用PHP计算此表中丢失的ID的百分比。

I am sure there are better ways and I am searching the best one, in example using only one query to directly receive the percentage value from the mysql query and with the fastest performance . 我确信有更好的方法,并且我正在寻找最好的方法,例如,仅使用一个查询 直接从mysql查询中接收百分比值 ,并且性能最快

Let's say my table name is my_table and the column with the values is named id . 假设我的表名称为my_table并且具有值的列名为id There are only unique ids in the column from 1 to *, and some ids are missing between. 从1到*的列中只有唯一的ID,并且之间缺少一些ID。 Example rows (values): 1 , 2 , 3 , 4 , 7 , 8 , 9 , 22, .... 示例行(值):1、2、3、4、7、8、9、22,...。

Which mysql query do you think would be the best way to directly receive the percentage of missing ids in this table? 您认为哪种mysql查询是直接接收此表中缺失ID百分比的最佳方法?

   SELECT (MAX(id)-count(id)) / MAX(id) * 100 as MissingRate
    FROM my_table

This will give you the correct percentage. 这将为您提供正确的百分比。 It will calculate it, based that you look every missing number like a missing number. 它将根据您将每个缺失的数字都看成一个缺失的数字进行计算。

SELECT Count(id)/MAX(id) as percent
    FROM my_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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