开发者

using SELECT inside INSERT query

please can anyone spot what is wrong with this query.It is giving error but i can't really see where the error is from since it is not really specified

mysql>

insert开发者_StackOverflow中文版 into sorting (mindlrid,maxdlrid)
values (
   (select min(dlrid) 
   from dlr 
   where sid=
     (select distinct(s.sendid)
      from sent s
           ,dlr d
      where s.uid=d.uid
      and d.d_billed=1
      and d.d_sent=0 
      and s.processed=1
      and s.sendid=d.sid
      order by s.sendid asc
      limit 1))
  ,(select max(dlrid)
    from dlr
    where sid=(
       select distinct(s.sendid)
       from sent s,dlr d
       where s.uid=d.uid
       and d.d_billed=1
       and d.d_sent=0
       and s.processed=1
       and s.sendid=d.sid
       order by s.sendid asc 
     limit 1)
   );

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


You're missing a closing parenthesis. Probably at the very end of the insert. Equivalent: there is an extra opening paren on the values clause.

INSERT INTO sorting
            (mindlrid,
             maxdlrid)
VALUES      (SELECT Min(dlrid)
              FROM   dlr
              WHERE  sid = (SELECT DISTINCT( s.sendid )
                            FROM   sent s,
                                   dlr d
                            WHERE  s.uid = d.uid
                                   AND d.d_billed = 1
                                   AND d.d_sent = 0
                                   AND s.processed = 1
                                   AND s.sendid = d.sid
                            ORDER  BY s.sendid ASC
                            LIMIT  1)),
             (SELECT Max(dlrid)
              FROM   dlr
              WHERE  sid = (SELECT DISTINCT( s.sendid )
                            FROM   sent s,
                                   dlr d
                            WHERE  s.uid = d.uid
                                   AND d.d_billed = 1
                                   AND d.d_sent = 0
                                   AND s.processed = 1
                                   AND s.sendid = d.sid
                            ORDER  BY s.sendid ASC
                            LIMIT  1)); 


You can use the result of a SELECT directly, no need to wrap that into a VALUES clause:

insert into sorting (mindlrid,maxdlrid)
select min(dlrid), max(dlrid)
from dlr 
where sid=
     (select distinct s.sendid
      from sent s
           ,dlr d
      where s.uid=d.uid
      and d.d_billed=1
      and d.d_sent=0 
      and s.processed=1
      and s.sendid=d.sid
      order by s.sendid asc
      limit 1)

Note that DISTINCT is not a function. It's an operator that includes all columns of the SELECT list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜