现要查询一个表,并得到符合下面二个条件中message中的内容;首先要找到符合条件字段(user),假如这个字段的内容为‘FA’;则满足第一个条件,第二个条件是,是这个表中满足第一个条件后,最后一个存入的数据(我这里用编号来判断;字段名为:send_id);之后我要将满足这两个条件的数据中的message内容附值给sendmessage;我是这样写的:
with adoquery1 do
begin
close;
sql.clear;
sql.add('select * from tsendm where max(send_id) and user='+quotedstr('FA'));
open;
sendmessage:=fieldbyname('message').asstring;
end;
不知道以上对不对,有其它看法的朋友提一下意见,因为没有装数据库,自已不好测试,所以写出来让大家看一下是否有问题,

解决方案 »

  1.   

    最后一个存入的数据
    ——————————
    不需要用max(send_id),order by desc 倒序排列
    这样第一条记录就是“最后一个存入的数据”
    ‘select * from tsendm where  user='+quotedstr('FA')+‘ order by desc’
      

  2.   

    max 不是这么用的,用了max 之后,要显示的字段必须 group by 才OKsql.add('select max(send_id) as max_id,message from tsendm where  user='+quotedstr('FA')+' Group By message');说实话这比较麻烦,不如用 send_id 从大到小排序取第一条记录sql.add('select top 1 * from tsendm where  user='+quotedstr('FA')+' Order By send_id DESC');