为什么在此查询结果中会出现重复的列?

huangapple go评论69阅读模式
英文:

Why do I get a duplicate column in the results of this query?

问题

当我提交这个查询(来自Codecademy课程),我会得到两个customer_id列,不明白为什么,如何去除重复的列?

表格截图

WITH previous_results AS (
  SELECT customer_id, COUNT(customer_id) FROM orders
  GROUP BY customer_id
)
SELECT * FROM previous_results
JOIN customers
  ON previous_results.customer_id = customers.customer_id;
英文:

When I submit this query (from codeacademy course) I get two customer_id columns and can't understand why, and how do I remove the dupe column?

Screen cap of tables

WITH previous_results AS (
  SELECT customer_id, COUNT(customer_id) FROM orders
  GROUP BY customer_id
)
SELECT * FROM previous_results
JOIN customers
  ON previous_results.customer_id = customers.customer_id;

答案1

得分: 1

你正在使用SELECT *,而Customer_ID在你的CTE "previous_results"和客户表中都存在。SELECT *将返回两者的所有列。请指定列名。

英文:

You are using SELECT * and Customer_ID exists in both your CTE "previous_results" and the customer table. SELECT * will return all columns from both. Specify your column names.

答案2

得分: 1

以下是要翻译的内容:

你在查询结果中出现两个customer_id列的原因是你选择了previous_results和customers表的所有列。由于这两个表都有一个customer_id列,因此最终会得到两个具有相同名称的列。

为了删除重复的customer_id列,你需要明确列出你想要选择的列,并排除其中一个customer_id列。如果需要,你可以使用SQL的AS关键字来重命名所选列。

以下是更新后的查询版本,仅选择必要的列并重命名它们,以避免重复的列名:

WITH previous_results AS (
SELECT customer_id, COUNT(customer_id) AS order_count
FROM orders
GROUP BY customer_id
)
SELECT customers.customer_id, customers.name, previous_results.order_count
FROM previous_results
JOIN customers ON previous_results.customer_id = customers.customer_id;

在这个版本中,我们仅从customers表中选择了customer_id和name列,以及从previous_results子查询中选择了customer_id和order_count列。我们还使用AS关键字将COUNT(customer_id)列重命名为order_count,这样可以更容易阅读查询结果。

英文:

The reason why you're getting two customer_id columns in your query result is that you are selecting all columns from both previous_results and customers tables. Since both tables have a customer_id column, you end up with two columns with the same name.

To remove the duplicate customer_id column, you need to explicitly list the columns you want to select and exclude one of the customer_id columns. You can use the SQL AS keyword to rename the selected columns if needed.

Here's an updated version of your query that selects only the necessary columns and renames them to avoid duplicate column names:

WITH previous_results AS (
  SELECT customer_id, COUNT(customer_id) AS order_count
  FROM orders
  GROUP BY customer_id
)
SELECT customers.customer_id, customers.name, previous_results.order_count
FROM previous_results
JOIN customers ON previous_results.customer_id = customers.customer_id;

In this version, we only select the customer_id and name columns from the customers table, and the customer_id and order_count columns from the previous_results subquery. We also use the AS keyword to rename the COUNT(customer_id) column to order_count, which makes it easier to read the query result.

huangapple
  • 本文由 发表于 2023年3月31日 03:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定