表1:
+-----------+----------+
| CODE | TYPEID |
+-----------+----------+
| 441 | mn014 |
| 223 | mn014 |
| 224 | mn014 |
| 655 | mn089 |
| 854 | mn089 |
| 449 | mn032 |
+-----------+----------+
TABLE2:
+-----------+----------+----------+
| CODE | TAKENDTE | RTURNDTE |
+-----------+----------+----------+
| 441 | 25/08/14 | 01/01/15 |
| 223 | 25/08/14 | 03/01/15 |
| 223 | 25/08/14 | 01/02/15 |
| 223 | 25/08/14 | NULL |
| 655 | 25/08/14 | 07/01/15 |
| 854 | 25/08/14 | NULL |
| 449 | 25/08/14 | 06/01/15 |
+-----------+---------------------+
我选择了typeid mn014的所有代码,这些代码要么不存在于第二个表中,要么在表2中存在的所有实例中都不是空的RTURNDTE列 用这个:
select t1.*
from table1 t1
where typeid = 'mn014' and
not exists (select 1
from table2 t2
where t2.code = t.code and
t2.rturndte is null
);
现在我无法想象如何选择最后选择的一个COD
任何想法?
答案 0 :(得分:1)
如果您想要任何单个结果,请在查询末尾添加ORDER BY RAND() LIMIT 1
。
如果您要查找特定代码值,请在查询末尾添加AND CODE = 'my code value'
。
答案 1 :(得分:0)
您可以LIMIT
结果集,ORDER BY
列可以在限制前对结果集进行排序:
select t1.*
from table1 t1
where typeid = 'mn014' and
not exists (select 1
from table2 t2
where t2.code = t.code and
t2.rturndte is null
);
LIMIT 1
ORDER BY some_column