2011-12-28 110 views
0

我有以下查詢,每當我運行它,它給了我不正確的語法附近「作爲」語法不正確

select 
    count(id_cat) as catcnt, 
    id_cat, ccatname, ccatpermalink, 
    (select ccatname 
    from btnn_classifiedcategory 
    where syncoperation <>'D' and id_ccat = id_cat) as catname 
from 
    btnn_myclassifides 
where 
    syncoperation <> 'D' 
    and Expirydate >= cast(convert(varchar(100), getdate(), 101) as datetime) 
    and id_cat in (select id_ccat 
        from BTNN_ClassifiedCategory 
        where syncoperation <> 'D' and id_ccat = bcc.id_ccat) as cnt 
        from BTNN_ClassifiedCategory bcc 

據我查詢請幫我

回答

1

查詢的這最後一部分不工作了,在所有:

and id_cat in (select id_ccat 
        from BTNN_ClassifiedCategory 
        where syncoperation <> 'D' and id_ccat = bcc.id_ccat) as cnt 
        from BTNN_ClassifiedCategory bcc 

不應該有此子查詢後as cnt,以及額外的from BTNN_ClassifiedCategory bcc不適合在任何地方都.....

這是否查詢工作:

select 
    count(id_cat) as catcnt, 
    id_cat, ccatname, ccatpermalink, 
    (select ccatname 
    from btnn_classifiedcategory 
    where syncoperation <>'D' and id_ccat = id_cat) as catname 
from 
    btnn_myclassifides 
where 
    syncoperation <> 'D' 
    and Expirydate >= cast(convert(varchar(100), getdate(), 101) as datetime) 
    and id_cat in (select id_ccat 
        from BTNN_ClassifiedCategory AS bcc 
        where syncoperation <> 'D' and id_ccat = bcc.id_ccat) 
+0

現在給出多部分標識符「bcc.id_ccat」無法綁定,列名'ccatname'無效,列名'ccatpermalink'無效。錯誤marc – Aties 2011-12-28 09:40:37

+0

@Aties:更新了我的回覆 - 現在能工作嗎?你的整個查詢是相當混亂的....爲了確保你選擇了正確的東西,你應該爲所涉及的所有表擁有**正確的表別名......我只是猜測在這裏,因爲我不知道你的桌子結構和你真的想做什麼... – 2011-12-28 09:42:34

+0

沒有傢伙不工作,但thnx兄弟的幫助。 – Aties 2011-12-28 09:45:32

0

你不需要最後as cnt最後from BTNN_ClassifiedCategory bcc

如果你保證第一子查詢的回報可能是更好的只有1排爲:

(select top 1 ccatname 
    from btnn_classifiedcategory 
    where syncoperation <>'D' and id_ccat = id_cat) as catname