Mat*_*yr' 5 php mysql select notin
我在MySql中有一个表,我保存了一些数据,让我们假设a name和a stand.我知道看台将从1到100,我想选择那些未被占用的看台.例如,让我们假设只有5个看台和这个表:
| name | stand |
--------------------
| test | 1 |
| anot | 3 |
| blah | 4 |
| uuuh | 5 |
Run Code Online (Sandbox Code Playgroud)
在这种情况下,唯一的免费立场将是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;
Run Code Online (Sandbox Code Playgroud)
这尚未经过测试,因此可能存在语法错误。
也就是说,创建一个包含所有可能值(1 到 100)的表。将此添加到您的表中。这将为您提供所有未使用的数字。但是,您希望将其限制为最小值和最大值,因此计算这些值并将其用于过滤。