加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 数据库 > MySql > 正文

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

发布时间:2020-10-19 07:56:08 所属栏目:MySql 来源:互联网
导读:我在MySql中有一个表,我保存了一些数据,让我们假设一个名字和一个立场.我知道看台将从1到100,我想选择那些未被占用的看台.例如,让我们假设只有5个看台和这个表:| name | stand | -------------------- | test | 1 | | anot | 3 | | blah |

我在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)的表.左边加入你的桌子.这将为您提供未使用的所有数字.但是,您希望将其限制为最小值和最大值,因此请计算这些值并将其用于过滤器.

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读