借助 WHERE 子句,您可以在为相关请求过滤数据时指定要应用的条件。使用 WHERE 子句时,可以指定一个或多个条件,并使用 AND 将其分隔开。每个条件都应遵循 field_name Operator value 这一格式。任何细分字段都可在 WHERE 子句中使用,但指标字段需要在 SELECT 子句中指定,才能在 WHERE 子句中使用。查询中WHERE子句是必需的,因为您必须始终指定要返回效果数据的日期范围。
ORDER BY 子句用于指定结果的返回顺序。这样一来,您就可以根据字段名称按升序或降序排列相关数据。每次排序都按 field_name 后跟 ASC 或 DESC 的形式指定。如果未指定 ASC 和 DESC,则默认顺序为 ASC。只有在 SELECT 子句中指定的字段才能在 ORDER BY 子句中使用。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"]],["最后更新时间 (UTC):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."]]