WHERE 子句會指定篩選要求資料時要套用的條件。使用 WHERE 子句時,可以指定一或多個條件,並使用 AND 分隔。每個條件都應採用 field_name Operator value 格式。任何區隔欄位都可用於 WHERE 子句,但指標欄位必須在 SELECT 子句中指定,才能用於 WHERE 子句。查詢時WHERE子句為必要,因為您一律必須指定要傳回成效資料的日期範圍。
ORDER BY 子句會指定傳回結果的順序。這可讓您依據欄位名稱,以遞增或遞減順序排列資料。每個排序都會指定為 field_name,後面接著 ASC 或 DESC。如未指定 ASC 和 DESC,則預設順序為 ASC。ORDER BY 子句只能使用 SELECT 子句中指定的欄位。查詢中的 ORDER BY 子句為選用項目。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-13 (世界標準時間)。"],[[["\u003cp\u003eThe Merchant API is the new version of the Content API for Shopping and offers improvements to integrations.\u003c/p\u003e\n"],["\u003cp\u003eUse Merchant Center Query Language to construct queries for metric and segment fields using clauses like \u003ccode\u003eSELECT\u003c/code\u003e, \u003ccode\u003eFROM\u003c/code\u003e, \u003ccode\u003eWHERE\u003c/code\u003e, \u003ccode\u003eORDER BY\u003c/code\u003e, and \u003ccode\u003eLIMIT\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eWHERE\u003c/code\u003e clause is required in queries to specify conditions, including the mandatory date range for performance data retrieval.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eORDER BY\u003c/code\u003e allows arranging results in ascending or descending order based on selected fields.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eLIMIT\u003c/code\u003e clause helps to specify the number of results to be returned, truncating the result set.\u003c/p\u003e\n"]]],["The Merchant API's beta version allows sending queries for metric and segment fields using the `reports.search` method. Queries are constructed with clauses like `SELECT`, `FROM`, `WHERE`, `ORDER BY`, and `LIMIT`. `SELECT` defines fields to fetch, `FROM` specifies the data table (currently `MerchantPerformanceView`), and `WHERE` filters data, requiring a date range. `ORDER BY` sorts results, and `LIMIT` restricts the number of returned results. The `WHERE` clause is mandatory and `ORDER BY` is optional.\n"],null,["# Query structure\n\nQueries for metric and segment fields can be sent to the [`reports.search`](/shopping-content/reference/rest/v2.1/reports/search)\nmethod. To construct a query in the Merchant Center Query Language, you need to first compose the\nquery using the [language grammar](/shopping-content/guides/reports/query-language/grammar).\nA query is made up of a number of clauses:\n\n- `SELECT`\n- `FROM`\n- `WHERE`\n- `ORDER BY`\n- `LIMIT`\n\nClauses use *field names* , *table names* , *operators* , *conditions* , and\n*orderings* to help you select the desired data. Once combined into a single\nquery, a request can be made using the Google Content API for Shopping. Let's look at how each\nclause can be used.\n\nClauses\n-------\n\n### SELECT\n\nThe `SELECT` clause specifies a set of fields to fetch in the request. `SELECT`\ntakes a comma-separated list of segment fields and metrics, returning the values\nin the response. The `SELECT` clause is **required** in a query.\n\nHere is a sample query that selects click metrics from a given table: \n\n SELECT\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n\nYou can also query different field types in a single request: \n\n SELECT\n segments.date,\n segments.program,\n metrics.impressions,\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n\n- Segment fields\n\n - `segments.date`\n - `segments.program`\n- Metrics\n\n - `metrics.impressions`\n - `metrics.clicks`\n\nSome fields are not allowed in the `SELECT` clause due to the following\nrestriction:\n\n- Querying segment fields without at least one metric field.\n\nInformation related to the above condition can be found in our reference docs.\n\n### FROM\n\nThe `FROM` clause specifies the table to fetch data from in the request. The\ntable in the `FROM` clause defines what fields can be used by all of the other\nclauses for the given query. Only a single table can be specified in the\n`FROM` clause. Currently, only the MerchantPerformanceView table is supported.\nThe `FROM` clause is **required** in a query to the `search` method on the\n`reports` service.\n\n### WHERE\n\nThe `WHERE` clause specifies conditions to apply when filtering data for the\nrequest. When using the `WHERE` clause, one or more conditions can be specified\nusing `AND` to separate them. Each condition should follow the pattern\n`field_name Operator value`. Any segments field can be used in the `WHERE`\nclause, but metrics fields need to be specified in the `SELECT` clause to be\nused in the `WHERE` clause. The `WHERE` clause is **required** in a query,\nbecause you must always specify the date range for when you want your\nperformance data returned.\n\nThe following is an example of using `WHERE` to return metrics from a given time\nperiod: \n\n SELECT\n segments.offer_id,\n metrics.impressions\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n\nYou can combine multiple conditions to filter the data. This example will return\nthe number of clicks per offer for the SHOPPING_ADS program where the clicks \\\u003e\n100 within the given 30-day period. \n\n SELECT\n segments.offer_id,\n segments.program,\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE metrics.clicks \u003e 100\n AND segments.program = SHOPPING_ADS\n AND segments.date BETWEEN '2020-08-01' AND '2020-08-31';\n\nIn the following query, you'll notice that `segments.date` was selected.\nRegardless about whether you select `segments.date`, a finite date range in the\n`WHERE` clause always has to be provided to retrieve performance data. \n\n SELECT\n segments.date,\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n\n| **Key Point:** The separator `AND` can only be used between operator conditions (`WHERE segments.program != \"SHOPPING_ADS\" AND segments.program != \"SURFACES_ACROSS_GOOGLE\"`), and not within a single operator condition (`WHERE segments.program != \"SHOPPING_ADS\" AND \"SURFACES_ACROSS_GOOGLE\"`),\n\nWhen filtering, the case-sensitivity of your operator is important to keep in\nmind.\n\nFor a complete list of operators, consult the\n[language grammar](/shopping-content/guides/query-language/grammar).\n\n### ORDER BY\n\nThe `ORDER BY` clause specifies the order in which the results are to be\nreturned. This allows you to arrange the data in ascending or descending order\nbased on a field name. Each ordering is specified as a `field_name` followed by\n`ASC` or `DESC`. If neither `ASC` nor `DESC` is specified, the order defaults\nto `ASC`. Only fields specified in the `SELECT` clause can be used in the\n`ORDER BY` clause. The `ORDER BY` clause is **optional** in a query.\n\nThe following query orders the returned rows by number of clicks from highest to\nlowest: \n\n SELECT\n segments.offer_id,\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n ORDER BY metrics.clicks DESC\n\nYou can specify multiple fields in the `ORDER BY` clause using a comma-separated\nlist. The ordering will occur in the same sequence as specified in the query.\nFor example, in this query, the results will be sorted in ascending order by\n`offer_id`, then in descending order by number of impressions, then in\ndescending order by number of clicks: \n\n SELECT\n segments.offer_id,\n metrics.impressions,\n metrics.clicks\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n ORDER BY\n segments.offer_id,\n metrics.impressions DESC,\n metrics.clicks DESC\n\n### LIMIT\n\nThe `LIMIT` clause allows you to specify the number of results to be returned.\nThis is useful if you're only interested in a summary.\n\nFor example, `LIMIT` can be used to restrict the total number of results for the\nfollowing query: \n\n SELECT\n segments.program,\n segments.offer_id,\n metrics.impressions\n FROM MerchantPerformanceView\n WHERE segments.date BETWEEN '2020-08-01' AND '2020-08-31'\n ORDER BY metrics.impressions DESC\n LIMIT 50\n\n| **Key Point:** `LIMIT` allows you to truncate the results. Use the `page_size` field to handle long lists of results."]]