1

全て、

多くの調査により、次のクエリを機能させることができましたが、それが何をしているのか完全には理解していません。私は今それを少し適応させる必要があるので、それをより完全に理解する必要があります.

クエリは複数のテーブルを検索して、結果ごとに 1 行 (情報の重複が多すぎる) ではなく、結果のグループごとに 1 行の結果のリストを提供します。

クエリは次のとおりです。

SELECT od_date, tbl_order.od_id, tbl_order_item.od_item_id, pd_code, item_code, pd_name, >cust, pd_type, tbl_order_item.od_item_id, tbl_order_item.test_suite_name,
MAX( IF( tbl_lab_item.test_name = 'Test 1', tbl_lab_item.test_result, NULL ) ) AS TEST1,
MAX( IF( tbl_lab_item.test_name = 'Test 2', tbl_lab_item.test_result, NULL ) ) AS TEST2,
MAX( IF( tbl_lab_item.test_name = 'Test 3', tbl_lab_item.test_result, NULL ) ) AS TEST3,
MAX( IF( tbl_lab_item.test_name = 'Test 4', tbl_lab_item.test_result, NULL ) ) AS TEST4,
MAX( IF( tbl_lab_item.test_name = 'Test 5', tbl_lab_item.test_result, NULL ) ) AS TEST5,
MAX( IF( tbl_lab_item.test_name = 'Test 6', tbl_lab_item.test_result, NULL ) ) AS TEST6
FROM tbl_item 
INNER JOIN tbl_item ON tbl_lab_item.od_item_id = tbl_item.od_item_id
INNER JOIN tbl_order ON tbl_order.od_id = tbl_item.od_id
WHERE tbl_order.od_date LIKE  '%$orderDate%'
AND customer_id = '%custID%'
GROUP BY tbl_lab_item.od_item_id

これにより、次の出力が得られます。

Order Date | Order ID | Item Id | <snip - other columns> | TEST1 | TEST2 | TEST 3 ...etc
09/09/2013 |    2     |    1    |                        | 10    | 20    | 30  ...etc

ここでやりたいことは、実際の数値を表示するのではなく、50 未満の結果を「< 50」として表示することです。「>100」についても同様です。

このロジックを上記のクエリに追加する方法、またはそれが可能かどうかさえわかりません。

あなたが提供できるどんな助けも大歓迎です。

どうもありがとう、ジェイソン

4

2 に答える 2

0

次のようなものを確認する必要がある各列にケースを使用してみてください

case 
    when  result <50 then '<50'
    when  result > 100 then '..'
end
于 2013-09-10T13:04:01.660 に答える
0

既存のクエリを次のような別のクエリにラップしたいようです...

select
      PQ.*,
      case when PQ.Test1 IS NULL then '     '
           when PQ.Test1 < 50 then '< 50'
           when PQ.Test1 > 100 then '> 100'
           else PQ.Test1 end as FinalTest1,
      case when PQ.Test2 IS NULL then '     '
           when PQ.Test2 < 50 then '< 50'
           when PQ.Test2 > 100 then '> 100'
           else PQ.Test2 end as FinalTest2,
      case when PQ.Test3 IS NULL then '     '
           when PQ.Test3 < 50 then '< 50'
           when PQ.Test3 > 100 then '> 100'
           else PQ.Test3 end as FinalTest3,
      case when PQ.Test4 IS NULL then '     '
           when PQ.Test4 < 50 then '< 50'
           when PQ.Test4 > 100 then '> 100'
           else PQ.Test4 end as FinalTest4,
      case when PQ.Test5 IS NULL then '     '
           when PQ.Test5 < 50 then '< 50'
           when PQ.Test5 > 100 then '> 100'
           else PQ.Test5 end as FinalTest5,
      case when PQ.Test6 IS NULL then '     '
           when PQ.Test6 < 50 then '< 50'
           when PQ.Test6 > 100 then '> 100'
           else PQ.Test6 end as FinalTest6
   from
      ( Your Full Query ) as PQ

コメントごとのフォローアップ

クエリがレコードを取得している場合、私のバージョンも取得する必要があります。元のクエリから、列の名前変更に対処するために、調整します

から

MAX( blah blah ) AS TEST1,

MAX( blah blah ) AS preTEST1,

列Test2-6も同様

次に、上記のクエリを

  case when PQ.preTest1 IS NULL then '     '
       when PQ.preTest1 < 50 then '< 50'
       when PQ.preTest1 > 100 then '> 100'
       else PQ.preTest1 end as Test1,

2-6など。それ以外の場合、元のクエリの残りの部分は変更されません...それはまだクエリの結果に依存しています。既存の投稿を編集して、修正したクエリをそこに置いて、私が見られるようにすることもできます。内側部分のクエリが何も返さない場合、外側のクエリも返されません。

于 2013-09-10T13:11:21.677 に答える