问题问的不清楚,可能这是你想要得select table1.*,table2.* from table1,table2 where 条件 order by table1.field1 aschttp://www.5dhome.net/blog/category/sql-language/

解决方案 »

  1.   

    哦,是這樣
    先得到單一的客戶名
    select distinct client from table where client!='' order by client
    再得到銷貨金額
    select sum(fsamo) as fsamo_a from table where fiono='52' and fcbno='$rs->client'
    再得到銷貨退回金額
    select sum(fsamo) as fsamo_b from table where fiono='02' and fcbno='$rs->client'
    最後得到真正的銷貨金額,用銷貨金額減去銷貨退回金額
    $fsamo=$rs1->fsamo_a-$rs2->fsamo_b我現在要按$fsamo排序
    怎麼辦?
      

  2.   

    select fcbno,(sum(if(fiono='52,fsamo,0))-sum(if(fiono='02',fsamo,0))) as total from table group by fcbno group by total
      

  3.   

    假如你的fiono只有52和02两种状态,还可以简化如下:select fcbno,sum(if(fiono='52,fsamo,-fsamo)) as total from table group by fcbno group by total
      

  4.   

    打错字了,后一个group by应为order by,如下:select fcbno,sum(if(fiono='52,fsamo,-fsamo)) as total from table group by fcbno order by total
      

  5.   

    說明一下,我用的是MsSQL2000,這樣執行出錯了
      

  6.   

    把 IF(expr1,expr2,expr3)
    换成 CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END
      

  7.   

    php版……不特别说明就默认mysql了……
      

  8.   

    select fcbno,
    (sum(CASE fiono WHEN '52' THEN fsamo)
    -
    sum(CASE fiono WHEN '02' THEN fsamo)) as total
    from c14 group by fcbno order by total你們看我這樣改的
    出錯
    找不出問題來
    伺服器: 訊息 170,層級 15,狀態 1,行 2
    行 2: ')' 附近的語法不正確。
      

  9.   

    select fcbno,(sum(CASE fsamo WHEN fiono='52' THEN 0 END)-sum(CASE fsamo WHEN fiono='02' THEN 0 END)) as total from c14 group by fcbno order by total试姐各
      

  10.   

    select fcbno,(sum(CASE fsamo WHEN fiono='52' THEN 0 END)-sum(CASE fsamo WHEN fiono='02' THEN 0 END)) as total from c14 group by fcbno order by total
    這個出錯,
    伺服器: 訊息 170,層級 15,狀態 1,行 1
    行 1: '=' 附近的語法不正確。我把我寫的那個加上了END
    select fcbno,
    (sum(CASE fiono WHEN '52' THEN fsamo END)
    -
    sum(CASE fiono WHEN '02' THEN fsamo END)) as total
    from c14 group by fcbno order by total
    結果total是NULL
      

  11.   

    順便看了下幫助
    CASE
    評估條件清單並傳回多種可能的結果運算式之一。 CASE 分為兩種格式: 簡單 CASE 函數會比較運算式與一組簡單運算式,以決定其結果。 
    搜尋 CASE 函數會評估一組布林運算式,以決定其結果。 
    兩種格式都支援選用的 ELSE 引數。 語法
    簡單 CASE 函數:CASE input_expression
        WHEN when_expression THEN result_expression
            [ ...n ]
        [ 
            ELSE else_result_expression
        END搜尋 CASE 函數:CASE
        WHEN Boolean_expression THEN result_expression
            [ ...n ]
        [ 
            ELSE else_result_expression
        END引數
    input_expression是使用簡單 CASE 格式時評估的運算式。input_expression 是任何有效的 Microsoft® SQL Server™ 運算式。 WHEN when_expression是使用簡單 CASE 格式時要比較 input_expression 的簡單運算式。when_expression 是任何有效的 SQL Server 運算式。input_expression 和各 when_expression 的資料型別必須相同,或者必須為隱含轉換。 n是指出可用多重 WHEN when_expression THEN result_expression 子句或多重 WHEN Boolean_expression THEN result_expression 子句的替代符號 (placeholder)。THEN result_expression是 input_expression 等於 when_expression 評估為 TRUE 時,或 Boolean_expression 評估為 TRUE 時傳回的運算式。result expression 是任何有效的 SQL Server 運算式。 ELSE else_result_expression是無任何比較運算評估為 TRUE 時傳回的運算式。如果忽略這個引數,且無任何比較運算評估為 TRUE,則 CASE 會傳回 NULL。else_result_expression 是任何有效的 SQL Server 運算式。else_result_expression 和任何 result_expression 的資料型別必須相同,或者必須為隱含轉換。WHEN Boolean_expression是使用搜尋 CASE 格式時評估的布林運算式。Boolean_expression 是任何有效的 Boolean 運算式。 結果型別
    自 result_expressions 和選用的 else_result_expression 中的一組型別傳回最高優先權型別。若需詳細資訊,請參閱資料型別優先順序。 結果值
    簡單 CASE 函數:
    評估 input_expression,然後依指定次序評估各 WHEN 子句的 input_expression = when_expression。 
    傳回評估為 TRUE 的第一個 (input_expression = when_expression) 的 result_expression。 
    若無 input_expression = when_expression 評估為 TRUE,則在指定 ELSE 子句時,SQL Server 會傳回 else_result_expression;若未指定 ELSE 子句,則傳回 NULL 值。 
    搜尋 CASE 函數:
    依指定次序評估各 WHEN 子句的 Boolean_expression。
    傳回評估為 TRUE 的第一個 result_expression。 
    若無 Boolean_expression 評估為 TRUE,則在指定 ELSE 子句時,SQL Server 會傳回 else_result_expression;若未指定 ELSE 子句,則傳回 NULL 值。 
    範例
    A. 使用具有簡單 CASE 函數的 SELECT 陳述式
    在 SELECT 陳述式中,簡單 CASE 函數只能檢查等式,不可進行其他比較。這一範例使用 CASE 函數來改變書籍目錄的顯示方式,使它們更容易讓人瞭解。USE pubs
    GO
    SELECT   Category = 
          CASE type
             WHEN 'popular_comp' THEN 'Popular Computing'
             WHEN 'mod_cook' THEN 'Modern Cooking'
             WHEN 'business' THEN 'Business'
             WHEN 'psychology' THEN 'Psychology'
             WHEN 'trad_cook' THEN 'Traditional Cooking'
             ELSE 'Not yet categorized'
          END,
       CAST(title AS varchar(25)) AS 'Shortened Title',
       price AS Price
    FROM titles
    WHERE price IS NOT NULL
    ORDER BY type, price
    COMPUTE AVG(price) BY type
    GO
      

  12.   

    select fcbno,
    (sum(CASE fiono WHEN '52' THEN fsamo ELSE 0 END)
    -
    sum(CASE fiono WHEN '02' THEN fsamo ELSE 0 END)) as total
    from c14 group by fcbno order by total好像OK了
    有數據出來了
    待我看看正確性如何
      

  13.   

    select fcbno,(sum(CASE fiono WHEN '52' THEN fsamo else 0 END)-sum(CASE fiono WHEN '02' THEN fsamo else 0 END)) as total from c14 group by fcbno order by total再加上else
      

  14.   

    简单点写,应该写成这样。select fcbno,(sum(CASE fiono WHEN '52' THEN fsamo WHEN '02' THEN -fsamo else 0 END) as total from c14 group by fcbno order by total