php – 仅选择数组中不在表中的数字

我在MySql中有一个表,我保存了一些数据,让我们假设一个名字和一个立场.
我知道看台将从1到100,我想选择那些未被占用的看台.例如,让我们假设只有5个看台和这个表:

|  name  |  stand  |
--------------------
|  test  |    1    |
|  anot  |    3    |
|  blah  |    4    |
|  uuuh  |    5    |

在这种情况下,唯一的免费展位将是2.

有没有声明呢? …我正在考虑条款NOT IN但是我无法弄清楚代码……也许我可以在MySql中定义am Array吗?

最佳答案
如果您知道值为1到100,那么您可以这样做:

select n.num
from (select d1.d*10 + d2.d as n
      from (select 0 as d union all select 1 union all select 2 union all select 3 union all select 4 union all
            select 5 union all select 6 union all select 7 union all select 8 union all select 9
           ) d1 cross join
           (select 0 as d union all select 1 union all select 2 union all select 3 union all select 4 union all
            select 5 union all select 6 union all select 7 union all select 8 union all select 9
           ) d2
      ) nums left outer join
      stands s
      on s.stand = nums.n cross join
      (select min(stand) as minstand and max(stand) as maxstand from stands) const
where s.stand is null and nums.n between minstand and maxstand;

这未经过测试,因此可能存在语法错误.

也就是说,创建一个包含所有可能值(1到100)的表.左边加入你的桌子.这将为您提供未使用的所有数字.但是,您希望将其限制为最小值和最大值,因此请计算这些值并将其用于过滤器.

相关文章

navicat查看某个表的所有字段的详细信息 navicat设计表只能一...
文章浏览阅读4.3k次。转载请把头部出处链接和尾部二维码一起...
文章浏览阅读488次。恢复步骤概要备份frm、ibd文件如果mysql...
文章浏览阅读225次。当MySQL单表记录数过大时,增删改查性能...
文章浏览阅读1.5k次。Mysql创建、删除用户MySql中添加用户,新...
MySQL是一种开源的关系型数据库管理系统,被广泛应用于各类应...