1010309 就食品安全衛生提出質詢。就社會治安提出質詢。就毒品危害提出質詢。就稅制改革提出質詢。就醫療資源分配提出質詢。口頭答詢
使用 選取查詢 增加威力
description
Transcript of 使用 選取查詢 增加威力
使用選取查詢增加威力- Select Query
查 詢 (Query)• 查詢 (Query):
– 提供從一個以上的資料表查看特定資料的物件– 使用查詢來檢視、變更、和分析資料– 查看之資料可為
• 直接來自同一資料表 / 查詢的欄位資料• 經由關聯,來自不同資料表 / 查詢的欄位資料• 由多個欄位資料之計算 / 會整
– 查詢所得之資料類似一資料表,然並不存於資料庫中 → “ 記錄集” (Recordset)
選取查詢 (Select Query)
查 詢 分 類• 選取查詢 (Select Query)
– 從各個資料表 / 查詢選取相關資訊– SQL: SELECT … FROM … [WHERE …]
• 動作查詢 (Action Query)– 新增、更新、刪除資料– SQL: INSERT UPDATE
DELETE SELECT … INTO ...
Query - Access 操作• 查詢 Tab• 新增• 設計檢視
– 欄位清單– 設計格線
從單一資料表選取資料• 選取資料表 →新增物件按鈕
→ 查詢• 欄位 / 資料表 or * / 資料表
– 需要之欄位、排序之欄位、準則之欄位• 排序• 顯示• 設定欄位屬性
輸 入 選 取 準 則• 準則:• 或:• AND• OR
輸 入 選 取 準 則 ( 續 )• BETWEEN AND
– BETWEEN 10 AND 100– BETWEEN #10/1/1997# AND #10/31/1997#
• IN– IN (“ 春” , “ 夏” , “ 秋” , “ 冬” )
• LIKE– LIKE “[!0-9]*”– LIKE “[A-Z] [A-Z] [A-Z] - # # #”
日期、時間• Day(date) -- Returns a value from 1 through 31 for the
day of the month.• Month(date) -- Returns a value from 1 through 12 for the
month of the year. • Year(date) -- Returns a value from 100 through 9999 for
the year. • Weekday(date) -- Returns a value from 1 (Sunday)
through 7 (Saturday) for the day of the week. • Hour(date) -- Returns the hour (0 through 23). • Date( ) -- Returns the current system date.
Datepart(Interval, date)• interval 引數的設定值如下:
“yyyy” 年“q” 季“m” 月“y” 一年的日數“d” 日“w” 一週的日數“ww” 週“h” 時“n” 分“s” 秒
計算數值 (Calculating Values)• Examples:
– [StreetAddress] & ", " & [City] & ", " & [State] & [Zip]– [ContractPrice] * [NumberOfWeeks] * [Commission1%]– 0.5 * [Length]*[Width]– Datepart(“y”, [DateInfo])– CCur(CLng([Price]*[Amount]*100)/100)
• 欄位之引用– [ 欄名 ]– [ 資料表名 ] ! [ 欄名 ]
• [Table1]![Field3] + 0.5 * [Table2]![Field3]
Operators• + - * /+ - * /• \\ Rounds both numeric expressions to integers and divide
s the first integer by the second integer. The result is rounded to an integer.
• ^̂ Raises the first numeric expression to the power indicated by the second numeric expression.
• MODMOD Rounds both numeric expressions to integers, divides the first integer by the second integer, and returns the remainder.
• && Creates an extended text string by concatenating the first text string to the second text string.
運算式建立幫手• 設計格線空白欄位→建立幫手
指定欄位名稱• 設計格線→欄位:
• Example
新欄名:運算式
Area:Area: 0.5 * [Length]*[Width]Total:Total: [Price]*[Amount]
合 計 查 詢 (Total Queries)
• → 合計列 ( 顯示於設計格線 )• 選定群組來合計
– 群組群組 (Group By)(Group By)• 聚合函數 (Total Functions)
– 總計總計 (Sum)(Sum) 、平均、平均 (Avg)(Avg)– 最小值最小值 (Min)(Min) 、最大值、最大值 (Max)(Max)– 標準差標準差 (StDev)(StDev) 、變異數、變異數 (Var)(Var)– 筆數筆數 (Count)*(Count)* 、第一筆、第一筆 (First)(First) 、最後一筆、最後一筆 (Last)(Last)
* COUNT( * )
合計查詢 ( 續 )
• 運算式運算式– 於欄位表示此運算式 ( 類似“計算數值” )
• 從群組中選取記錄– 條件條件 → 準則
• 選定特定群組– 運算式 ┼ 準則運算式 ┼ 準則
使用查詢參數• 依使用者之輸入資料作為查詢之依據• 準則列
– [ 名稱 ] Ex: [InputVar]– [ 句子 ] Ex: [ 請輸入資料: ]
• 參數資料類型– 預設類型:文字– 變更參數資料類型:
• 查詢 → 參數
交叉資料表查詢
列名 (Row Heading)
欄名(Column Heading)
值 (Value)
不顯示(Not Show)
交叉資料表查詢設定• 列名
– 至少指定一欄位為列名– 合計列:群組、聚合函數、含聚合函數之運算式
• 欄名– 指定一欄位為欄名– 合計列:群組
• 值– 指定一欄位為欄名– 合計列:聚合函數、含聚合函數之運算式
多資料表查詢• 選擇多個資料表建立查詢
– “ 新增”按鈕– 從“顯示資料表”對話方塊,選擇資料表
• Join 種類– Inner Join– Outer Join: Left Join, Right Join
• 尋找不吻合資料查詢– = Outer Join + IsNull– 利用精靈
• 以查詢來建立查詢– 把已存在之查詢視為一般資料表來操作
使用查詢精靈
自定查詢屬性tableName.*
TOP nTOP n PERCENT
DISTINCT
DISTINCTROW
WHERE …
ORDER BY
對應的對應的 SQLSQL 參參數數
唯一值 / 唯一記錄• 唯一值 (Unique Value, DISTINCT)
– 查詢所得之記錄中,若有多筆記錄完全相同時,則僅會以一筆記錄顯示。• 唯一記錄 (Unique Record, DISTINCTROW)
– 查詢所得之記錄,對於有多筆顯示相同結果之記錄,是否以一筆記錄顯示,是依據底層資料來源中的全部欄位,而非只有出現於查詢本身的那些欄位。– 在多資料表查詢時,才有意義。