我在两个不同的表中有一个数字列。ID。7 d; l" x( Q) H+ B
我创建了这样一个工会: - s% q r; V& Z: v) {- D K7 `SELECT number FROM table1 UNION SELECT number FROM table ORDER BY number ASC结果如下: # M) L0 D8 D7 R( t: I4 ?! v/ x1 B7 ynumber=====123568如何找到最低未使用号码?在这种情况下,它将是4。一旦使用4,它将是7,等等。 2 p1 I( M7 d7 `$ O ! \) v' L; i$ F) J9 v6 c 解决方案: $ H* |, g2 [: w+ N i( s
假设您的电话号码始终从1开始,以下查询将给出未使用的电话号码" Q1 L# |" G# q7 k4 X
select min(rank) as Num from (select num,@curRank1 := @curRank1 1 AS rank from (SELECT num1 as num FROM t1 UNIONSELECT num2 as num FROM t2) a1,(SELECT @curRank1 := 0) r ORDER BY num ASC) tab where num != rank;`