oracle
  • select
  • oracle10g
  • 2012-07-18 67 views 1 likes 
    1

    我有一個表,mealdb,它有三個字段, "userid", "timeofday", "daydate"(全部爲varchar2)。 timeofday可以有兩個值,「中午」或「下午」。現在我想計算一下給定用戶的下午總數。我試過像Oracle數據庫中的複合選擇查詢

    select sum(timeofday) 
        from mealdb 
    where timeofday='afternoon' where userid='1200'; 
    

    但它在Oracle 10g中發生錯誤。

    我該如何解決這個問題?

    +0

    將'sum(timeofday)'改爲'count(1)'和'where timeofday ='afternoon'其中userid ='1200'; 'to'where timeofday ='afternoon'and userid ='1200'。我建議你通過基本的SQL教程,因爲過濾數據是編寫SQL的基本部分之一。 ' – Chandu 2012-07-18 16:14:47

    +0

    謝謝。我很早以前就學習了SQL,並且一直沒有聯繫好。感謝您的建議。 – Mistu4u 2012-07-18 16:18:03

    +0

    很抱歉,通知...但你提供的代碼沒有工作。它顯示了[code] select count(timeofday)from mealdb where userid ='mistu4u'; [code] as 4 like like already entered in my table.But when I添加子句[代碼] where timeofday ='下午'[代碼]它顯示0.請看看! – Mistu4u 2012-07-18 16:44:57

    回答

    1
    select count(1) 
        from mealdb 
    where timeofday='afternoon' and userid='1200'; 
    
    +0

    很抱歉,通知...但你提示的代碼沒有工作。它顯示['代碼']從mealdb的select count(timeofday)where userid ='mistu4u'; ['code'] as 4 like like already entered in my table 。但是當我添加子句['code'] where timeofday ='afternoon'['code']它顯示0.請看看! – Mistu4u 2012-07-18 16:47:24

    +0

    你可以指定mealdb表的內容是什麼嗎?如果您試圖檢索在您的問題中指定的數據,Query *應該*工作。 – 2012-07-18 19:15:35

    +0

    就像我在我的問題中已經描述的那樣,所有內容都在mealdb表中。感謝您的幫助! – Mistu4u 2012-07-19 05:38:31

    2

    只有一個WHERE關鍵字是允許的。

    而且條件是使用添加布爾運算符像ANDOR

    select count(*) 
    from mealdb 
    where timeofday='afternoon' 
        and userid='1200'; 
    
    0

    嘗試select count(timeofday) from mealdb where timeofday='afternoon' and userid='1200'您需要使用CountSum和使用and您的第二where是。

    相關問題