sql错误中的子查询

时间:2017-01-17 10:37:28

标签: sql-server subquery

我想应用子查询我这样做但这显示错误

select          t.Tid, 
            i.Description,          
            tst.Description,
            dd.DoID,
            dd.Document,
            dd.docname  

    from    Ticet t 
        (
            select tid, MIN(hid) hid from TtH th group by Tid
        ) mh
    inner join DocDown dd on t.clientid=dd.Clientid
    inner join Item i on t.Itemid=i.Itemid
    inner join TStat tst on t.Status=tst.Status
    where 
     t.Status in (1, 2 )
    and gu.Userid=1926

    order by t.Tid

错误

  

关键字“select”附近的语法不正确。

     

')'附近的语法不正确。

4 个答案:

答案 0 :(得分:0)

尝试以下查询,您错过了joint以及mh

上的select t.Tid, i.Description, tst.Description, dd.DoID, dd.Document, dd.docname from Ticet t inner join ( select tid, MIN(hid) hid from TtH th group by Tid ) mh on mh.tid = t.Tid inner join DocDown dd on t.clientid=dd.Clientid inner join Item i on t.Itemid=i.Itemid inner join TStat tst on t.Status=tst.Status where t.Status in (1, 2 ) and gu.Userid=1926 order by t.Tid 和条件
String str1 = "BCS/MS/13/09/0001";
int pos1= str1.indexOf("MS");
String str2 = "BCOM/MS/09/0149";
int pos2= str2.indexOf("MS");

答案 1 :(得分:0)

这个

   from    Ticet t 
    JOIN
        (
            select tid, MIN(hid) hid from TtH th group by Tid
        ) mh
on mh.somecol =t.somecol
and so on

应该是

{{1}}

答案 2 :(得分:0)

您需要加入子查询表...

假设mh.tid = t.tid是您想要的加入。

SELECT t.Tid, i.Description, tst.Description, dd.DoID, dd.Document, dd.docname  
FROM Ticet t 
INNER JOIN (SELECT tid, MIN(hid) hid FROM TtH th GROUP BY Tid) mh ON mh.tid = t.Tid
INNER JOIN DocDown dd ON t.clientid = dd.Clientid
INNER JOIN Item i ON t.Itemid = i.Itemid
INNER JOIN TStat tst ON t.Status = tst.Status
WHERE t.Status IN (1, 2)
AND gu.Userid = 1926
ORDER BY t.Tid

答案 3 :(得分:0)

请为mh添加子查询:

select t.Tid, i.Description, tst.Description, dd.DoID, dd.Document, dd.docname  
from   Ticet t 
inner join ( select tid, MIN(hid) hid from TtH th group by Tid ) as  mh on t.Tid=mh.tid
inner join DocDown dd on t.clientid=dd.Clientid
inner join Item i on t.Itemid=i.Itemid
inner join TStat tst on t.Status=tst.Status
where t.Status in (1, 2 ) and gu.Userid=1926
order by t.Tid