MYSQL - 正确连接我的 3 个表的语法
MYSQL - Syntax for Joining My 3 Tables Correctly
Edit2:选择将查询和 collate/handle 信息作为一个整体从数据库的输出中分离出来。以 .CSV 格式取出这些,并将它们添加到 Excel 中,我将在其中 运行 实际数字。
查询 1 以提取订单和所需信息:
SELECT
shipstation_orders_v2.id AS SSO_id,
shipstation_orders_v2.order_number AS SSO_orderNumber,
shipstation_orders_v2.order_id AS SSO_orderID,
shipstation_orders_v2.storename AS SSO_storeName,
shipstation_orders_v2.order_date AS SSO_orderDate,
shipstation_orders_v2.order_total AS SSO_orderTotal,
shipstation_orders_v2.name AS SSO_name,
shipstation_orders_v2.company AS SSO_company
FROM shipstation_orders_v2
GROUP BY shipstation_orders_v2.id,
shipstation_orders_v2.order_number,
shipstation_orders_v2.order_id,
shipstation_orders_v2.storename,
shipstation_orders_v2.order_date,
shipstation_orders_v2.order_total,
shipstation_orders_v2.name,
shipstation_orders_v2.company
ORDER BY SSO_orderDate
查询 2 以提取履行和等效信息:
SELECT DISTINCT
shipstation_orders_v2.id AS SSO_id,
shipstation_fulfillments.id AS SSF_id,
shipstation_fulfillments.order_number AS SSF_orderNumber,
shipstation_orders_v2.order_number AS SSO_orderNumber,
shipstation_orders_v2.order_id AS SSO_orderID,
shipstation_orders_v2.storename AS SSO_storeName,
shipstation_orders_v2.order_date AS SSO_orderDate,
shipstation_fulfillments.order_date AS SSF_orderDate,
shipstation_orders_v2.order_total AS SSO_orderTotal,
shipstation_fulfillments.amount_paid AS SSF_amountPaid,
shipstation_orders_v2.name AS SSO_name,
shipstation_orders_v2.company AS SSO_company,
shipstation_fulfillments.name AS SSF_name,
shipstation_fulfillments.company AS SSF_company
FROM shipstation_fulfillments
INNER JOIN shipstation_orders_v2
ON shipstation_fulfillments.order_number =
shipstation_orders_v2.order_number
WHERE shipstation_fulfillments.order_number =
shipstation_orders_v2.order_number
GROUP BY shipstation_orders_v2.id,
shipstation_fulfillments.id,
shipstation_fulfillments.order_number,
shipstation_orders_v2.order_number,
shipstation_orders_v2.order_id,
shipstation_orders_v2.storename,
shipstation_orders_v2.order_date,
shipstation_fulfillments.order_date,
shipstation_orders_v2.order_total,
shipstation_fulfillments.amount_paid,
shipstation_orders_v2.name,
shipstation_orders_v2.company,
shipstation_fulfillments.name,
shipstation_fulfillments.company
编辑:问题已标记为已回答。我想出了另一种不那么轻率的方法。 DRapp 让我的大脑动起来的道具。
原代码在文字墙下方
我是一个自学成才的 MySQL 数据库用户。我不会说管理员,因为它只是我。我已经建立了一个小型工作数据库 - 大约 60,000 行和最多 51 列分布在三个 table 中。我在工作中使用它来组织一个相当不同的销售数据设置,并利用它来识别趋势、季节性和所有这些好东西。我主要使用 Shipstation 数据。
我的问题是什么时候需要介绍第三个 table。有两个table,显然,它只是一个简单的JOIN
。我让它工作得很好。我在为第三个 table.
正确设置 JOINs
时遇到了一些麻烦
我正在尝试 JOIN
从最里面的两个查询到 shipstation_orders_v2
和 order_keys
的数据到我在第三个 [=74] 中得到的 shipstation_fulfillments
结果=].
对于那些不使用 Shipstation 或不熟悉它的这个元素的人来说,履行与订单属于不同的类别,并且不使用完全相同的数据。这是我将它们粘合在一起的肮脏方式,所以我们有一些关于销售和运输趋势等的体面的、可操纵的信息。
我正在进行从 shipstation_orders_v2
到 order_keys
的内部查询,作为 SELECT DISTINCT
拆分订单总和的一种方式。在建立该子查询之前,我遇到了数据重复问题。使用(现在的)子查询和子子查询,重复问题已被消除,只有这两个 tables 它工作正常。
问题是,当我使用 JOIN
从 shipstation_fulfillments
到子查询和子子查询创建 SELECT
时,我遇到了障碍。
我在处理此查询时遇到了几个错误。按发生顺序和解决顺序:
Error 2013
,在查询期间与服务器失去连接(这告诉我我正在对三个连接的 table 进行完整的 table 读取,因为它没有出错事先,但我的 rinkadink 设置无法处理它)。我摆脱了那个。
然后,未识别的 table 名称出现错误 1051 shipstation_fulfillments
。对我来说,我认为这可能是查询别名的问题。我不确定。
最后,好的 ole Error 1064
,在第一个子查询后的语法不正确
SELECT shipstation_fulfillments
个参数。
由于是自学成才,我几乎可以保证我只是在某处遗漏了一个语法元素,而对于 MySQL 的熟练用户来说,这似乎是相当明显的。以下是我当前的查询设置。
如果需要任何说明,请告诉我。
SELECT
`shipstation_fulfillments`.`order_date` AS `orderDate`,
`shipstation_fulfillments`.`order_number` AS `orderNumber`,
(`shipstation_fulfillments`.`amount_paid` + `shipstation_fulfillments`.`tax_paid`) AS "Total Paid",
`shipstation_fulfillments`.`name` AS `name`,
`shipstation_fulfillments`.`company` AS `company`,
FROM
(
(SELECT
COUNT(`shipstation_orders_v2`.`order_key`) AS `orderCount`,
`shipstation_orders_v2`.`key_id` AS `key_id`,
`shipstation_orders_v2`.`order_number` AS `order_number`,
MAX(`shipstation_orders_v2`.`order_date`) AS `order_date`,
`shipstation_orders_v2`.`storename` AS `store`,
(`shipstation_orders_v2`.`order_total` - `shipstation_orders_v2`.`shippingPaid`) AS `orderPrice`,
`shipstation_orders_v2`.`shippingpaid` AS `shippingPaid`,
SUM(`shipstation_orders_v2`.`shippingpaid`) AS `SUM shippingPaid`,
`shipstation_orders_v2`.`order_total` AS `orderTotal`,
SUM(`shipstation_orders_v2`.`order_total`) AS `SUM Total Amount Paid`,
`shipstation_orders_v2`.`qtyshipped` AS `qtyShipped`,
SUM(`shipstation_orders_v2`.`qtyshipped`) AS `SUM qtyShipped`,
`shipstation_orders_v2`.`name` AS `name`,
`shipstation_orders_v2`.`company` AS `company`
FROM
(SELECT DISTINCT
`order_keys`.`key_id` AS `key_id`,
`order_keys`.`order_key` AS `order_key`,
`shipstation_orders_v2`.`order_number` AS `order_number`,
`shipstation_orders_v2`.`order_id` AS `order_id`,
`shipstation_orders_v2`.`order_date` AS `order_date`,
`shipstation_orders_v2`.`storename` AS `storename`,
`shipstation_orders_v2`.`order_total` AS `order_total`,
`shipstation_orders_v2`.`qtyshipped` AS `qtyshipped`,
`shipstation_orders_v2`.`shippingpaid` AS `shippingpaid`,
`shipstation_orders_v2`.`name` AS `name`,
`shipstation_orders_v2`.`company` AS `company`
FROM
(`shipstation_orders_v2`
JOIN `order_keys` ON ((`order_keys`.`order_key` = `shipstation_orders_v2`.`order_id`)))) `t`)
JOIN `shipstation_fulfillments`
ON (`shipstation_orders_v2`.`order_number` = `shipstation_fulfillments`.`order_number`)) `w`
作为几点说明...至于长 table 名称,没问题,但您可以使用别名引用它们,例如我通过示例所做的...ShipStation_Fulfillments SSF。 .. "SSF" 现在是较短打字的别名,但仍然有意义。
当通过 "AS" 更改查询中的列名时,您只需要好像您的列名结果将与您在开始时的原始结果有所不同,例如 SSF.order_date AS orderDate,其中您从最后的列名中删除“_”,但也在 "Total Paid" 中删除(但我讨厌带有嵌入空格的列名,让用户界面处理标签的事情,但那只是我)。
当输入 table.column(或 alias.column)时,使用 CamelCasing 有助于提高可读性,而使用 camelcasing 则更难阅读,大脑会自然地为我们分解成可读的单词。
基于查询的其他问题。外部查询部分无法识别来自内部封闭查询的别名,只能识别子选择的别名,就像您使用 "t" 和 "w" 别名一样。
接下来,在执行 JOIN 时,我的偏好是以 table 在查询中的方式读取它们,在左侧列出第一个,在右侧列出任何连接到 TO 的内容。
如果从 Table A 加入到 Table B,ON 子句将是 ON A.KeyID = B.KeyID vs B.KeyID = A.KeyID 特别是如果你要去几个 tables...A->B, B->C, C->D
任何包含聚合(sum、avg、count、min、max 等)的查询都必须有一个 "GROUP BY" 子句来标识每条记录何时应该中断。在您的示例中,我假设原始销售订单中断。
虽然此查询不工作,但这里是您的查询的清理版本,显示了上面的实现。
SELECT
SSF.order_date AS OrderDate,
SSF.order_number AS OrderNumber,
(SSF.amount_paid + SSF.tax_paid) AS `Total Paid`,
SSF.name,
SSF.company
FROM
( SELECT
SSOv2.key_id,
SSOv2.order_number,
SSOv2.storename AS store,
SSOv2.order_total - SSOv2.shippingPaid AS OrderPrice,
SSOv2.ShippingPaid,
SSOv2.order_total AS OrderTotal,
SSOv2.QtyShipped,
SSOv2.name,
SSOv2.company,
COUNT(SSOv2.order_key) AS orderCount,
MAX(SSOv2.order_date) AS order_date,
SUM(SSOv2.shippingpaid) AS `SUM shippingPaid`,
SUM(SSOv2.order_total) AS `SUM Total Amount Paid`,
SUM(SSOv2.qtyshipped) AS `SUM qtyShipped`
FROM
( SELECT DISTINCT
OK.key_id AS key_id,
OK.order_key AS order_key,
SSOv2.order_number AS order_number,
SSOv2.order_id AS order_id,
SSOv2.order_date AS order_date,
SSOv2.storename AS storename,
SSOv2.order_total AS order_total,
SSOv2.qtyshipped AS qtyshipped,
SSOv2.shippingpaid AS shippingpaid,
SSOv2.name AS name,
SSOv2.company AS company
FROM
shipstation_orders_v2 SSOv2
JOIN order_keys
ON SSOv2.order_id = OK.order_key
JOIN shipstation_fulfillments SSF
ON SSOv2.order_number = SSF.order_number ) t
) w
接下来,在没有看到对解决查询至关重要的实际数据或列出的结构的情况下,我将要求您编辑现有的 post。创建示例 table 列表 table、列和示例数据,以便我们可以查看您正在聚合和尝试从查询中获取的内容的基础。特别显示每个订单和履行可能有多行的地方,以及您期望结果显示的示例答案。
Edit2:选择将查询和 collate/handle 信息作为一个整体从数据库的输出中分离出来。以 .CSV 格式取出这些,并将它们添加到 Excel 中,我将在其中 运行 实际数字。
查询 1 以提取订单和所需信息:
SELECT
shipstation_orders_v2.id AS SSO_id,
shipstation_orders_v2.order_number AS SSO_orderNumber,
shipstation_orders_v2.order_id AS SSO_orderID,
shipstation_orders_v2.storename AS SSO_storeName,
shipstation_orders_v2.order_date AS SSO_orderDate,
shipstation_orders_v2.order_total AS SSO_orderTotal,
shipstation_orders_v2.name AS SSO_name,
shipstation_orders_v2.company AS SSO_company
FROM shipstation_orders_v2
GROUP BY shipstation_orders_v2.id,
shipstation_orders_v2.order_number,
shipstation_orders_v2.order_id,
shipstation_orders_v2.storename,
shipstation_orders_v2.order_date,
shipstation_orders_v2.order_total,
shipstation_orders_v2.name,
shipstation_orders_v2.company
ORDER BY SSO_orderDate
查询 2 以提取履行和等效信息:
SELECT DISTINCT
shipstation_orders_v2.id AS SSO_id,
shipstation_fulfillments.id AS SSF_id,
shipstation_fulfillments.order_number AS SSF_orderNumber,
shipstation_orders_v2.order_number AS SSO_orderNumber,
shipstation_orders_v2.order_id AS SSO_orderID,
shipstation_orders_v2.storename AS SSO_storeName,
shipstation_orders_v2.order_date AS SSO_orderDate,
shipstation_fulfillments.order_date AS SSF_orderDate,
shipstation_orders_v2.order_total AS SSO_orderTotal,
shipstation_fulfillments.amount_paid AS SSF_amountPaid,
shipstation_orders_v2.name AS SSO_name,
shipstation_orders_v2.company AS SSO_company,
shipstation_fulfillments.name AS SSF_name,
shipstation_fulfillments.company AS SSF_company
FROM shipstation_fulfillments
INNER JOIN shipstation_orders_v2
ON shipstation_fulfillments.order_number =
shipstation_orders_v2.order_number
WHERE shipstation_fulfillments.order_number =
shipstation_orders_v2.order_number
GROUP BY shipstation_orders_v2.id,
shipstation_fulfillments.id,
shipstation_fulfillments.order_number,
shipstation_orders_v2.order_number,
shipstation_orders_v2.order_id,
shipstation_orders_v2.storename,
shipstation_orders_v2.order_date,
shipstation_fulfillments.order_date,
shipstation_orders_v2.order_total,
shipstation_fulfillments.amount_paid,
shipstation_orders_v2.name,
shipstation_orders_v2.company,
shipstation_fulfillments.name,
shipstation_fulfillments.company
编辑:问题已标记为已回答。我想出了另一种不那么轻率的方法。 DRapp 让我的大脑动起来的道具。
原代码在文字墙下方
我是一个自学成才的 MySQL 数据库用户。我不会说管理员,因为它只是我。我已经建立了一个小型工作数据库 - 大约 60,000 行和最多 51 列分布在三个 table 中。我在工作中使用它来组织一个相当不同的销售数据设置,并利用它来识别趋势、季节性和所有这些好东西。我主要使用 Shipstation 数据。
我的问题是什么时候需要介绍第三个 table。有两个table,显然,它只是一个简单的JOIN
。我让它工作得很好。我在为第三个 table.
JOINs
时遇到了一些麻烦
我正在尝试 JOIN
从最里面的两个查询到 shipstation_orders_v2
和 order_keys
的数据到我在第三个 [=74] 中得到的 shipstation_fulfillments
结果=].
对于那些不使用 Shipstation 或不熟悉它的这个元素的人来说,履行与订单属于不同的类别,并且不使用完全相同的数据。这是我将它们粘合在一起的肮脏方式,所以我们有一些关于销售和运输趋势等的体面的、可操纵的信息。
我正在进行从 shipstation_orders_v2
到 order_keys
的内部查询,作为 SELECT DISTINCT
拆分订单总和的一种方式。在建立该子查询之前,我遇到了数据重复问题。使用(现在的)子查询和子子查询,重复问题已被消除,只有这两个 tables 它工作正常。
问题是,当我使用 JOIN
从 shipstation_fulfillments
到子查询和子子查询创建 SELECT
时,我遇到了障碍。
我在处理此查询时遇到了几个错误。按发生顺序和解决顺序:
Error 2013
,在查询期间与服务器失去连接(这告诉我我正在对三个连接的 table 进行完整的 table 读取,因为它没有出错事先,但我的 rinkadink 设置无法处理它)。我摆脱了那个。
然后,未识别的 table 名称出现错误 1051 shipstation_fulfillments
。对我来说,我认为这可能是查询别名的问题。我不确定。
最后,好的 ole Error 1064
,在第一个子查询后的语法不正确
SELECT shipstation_fulfillments
个参数。
由于是自学成才,我几乎可以保证我只是在某处遗漏了一个语法元素,而对于 MySQL 的熟练用户来说,这似乎是相当明显的。以下是我当前的查询设置。
如果需要任何说明,请告诉我。
SELECT
`shipstation_fulfillments`.`order_date` AS `orderDate`,
`shipstation_fulfillments`.`order_number` AS `orderNumber`,
(`shipstation_fulfillments`.`amount_paid` + `shipstation_fulfillments`.`tax_paid`) AS "Total Paid",
`shipstation_fulfillments`.`name` AS `name`,
`shipstation_fulfillments`.`company` AS `company`,
FROM
(
(SELECT
COUNT(`shipstation_orders_v2`.`order_key`) AS `orderCount`,
`shipstation_orders_v2`.`key_id` AS `key_id`,
`shipstation_orders_v2`.`order_number` AS `order_number`,
MAX(`shipstation_orders_v2`.`order_date`) AS `order_date`,
`shipstation_orders_v2`.`storename` AS `store`,
(`shipstation_orders_v2`.`order_total` - `shipstation_orders_v2`.`shippingPaid`) AS `orderPrice`,
`shipstation_orders_v2`.`shippingpaid` AS `shippingPaid`,
SUM(`shipstation_orders_v2`.`shippingpaid`) AS `SUM shippingPaid`,
`shipstation_orders_v2`.`order_total` AS `orderTotal`,
SUM(`shipstation_orders_v2`.`order_total`) AS `SUM Total Amount Paid`,
`shipstation_orders_v2`.`qtyshipped` AS `qtyShipped`,
SUM(`shipstation_orders_v2`.`qtyshipped`) AS `SUM qtyShipped`,
`shipstation_orders_v2`.`name` AS `name`,
`shipstation_orders_v2`.`company` AS `company`
FROM
(SELECT DISTINCT
`order_keys`.`key_id` AS `key_id`,
`order_keys`.`order_key` AS `order_key`,
`shipstation_orders_v2`.`order_number` AS `order_number`,
`shipstation_orders_v2`.`order_id` AS `order_id`,
`shipstation_orders_v2`.`order_date` AS `order_date`,
`shipstation_orders_v2`.`storename` AS `storename`,
`shipstation_orders_v2`.`order_total` AS `order_total`,
`shipstation_orders_v2`.`qtyshipped` AS `qtyshipped`,
`shipstation_orders_v2`.`shippingpaid` AS `shippingpaid`,
`shipstation_orders_v2`.`name` AS `name`,
`shipstation_orders_v2`.`company` AS `company`
FROM
(`shipstation_orders_v2`
JOIN `order_keys` ON ((`order_keys`.`order_key` = `shipstation_orders_v2`.`order_id`)))) `t`)
JOIN `shipstation_fulfillments`
ON (`shipstation_orders_v2`.`order_number` = `shipstation_fulfillments`.`order_number`)) `w`
作为几点说明...至于长 table 名称,没问题,但您可以使用别名引用它们,例如我通过示例所做的...ShipStation_Fulfillments SSF。 .. "SSF" 现在是较短打字的别名,但仍然有意义。
当通过 "AS" 更改查询中的列名时,您只需要好像您的列名结果将与您在开始时的原始结果有所不同,例如 SSF.order_date AS orderDate,其中您从最后的列名中删除“_”,但也在 "Total Paid" 中删除(但我讨厌带有嵌入空格的列名,让用户界面处理标签的事情,但那只是我)。
当输入 table.column(或 alias.column)时,使用 CamelCasing 有助于提高可读性,而使用 camelcasing 则更难阅读,大脑会自然地为我们分解成可读的单词。
基于查询的其他问题。外部查询部分无法识别来自内部封闭查询的别名,只能识别子选择的别名,就像您使用 "t" 和 "w" 别名一样。
接下来,在执行 JOIN 时,我的偏好是以 table 在查询中的方式读取它们,在左侧列出第一个,在右侧列出任何连接到 TO 的内容。
如果从 Table A 加入到 Table B,ON 子句将是 ON A.KeyID = B.KeyID vs B.KeyID = A.KeyID 特别是如果你要去几个 tables...A->B, B->C, C->D
任何包含聚合(sum、avg、count、min、max 等)的查询都必须有一个 "GROUP BY" 子句来标识每条记录何时应该中断。在您的示例中,我假设原始销售订单中断。
虽然此查询不工作,但这里是您的查询的清理版本,显示了上面的实现。
SELECT
SSF.order_date AS OrderDate,
SSF.order_number AS OrderNumber,
(SSF.amount_paid + SSF.tax_paid) AS `Total Paid`,
SSF.name,
SSF.company
FROM
( SELECT
SSOv2.key_id,
SSOv2.order_number,
SSOv2.storename AS store,
SSOv2.order_total - SSOv2.shippingPaid AS OrderPrice,
SSOv2.ShippingPaid,
SSOv2.order_total AS OrderTotal,
SSOv2.QtyShipped,
SSOv2.name,
SSOv2.company,
COUNT(SSOv2.order_key) AS orderCount,
MAX(SSOv2.order_date) AS order_date,
SUM(SSOv2.shippingpaid) AS `SUM shippingPaid`,
SUM(SSOv2.order_total) AS `SUM Total Amount Paid`,
SUM(SSOv2.qtyshipped) AS `SUM qtyShipped`
FROM
( SELECT DISTINCT
OK.key_id AS key_id,
OK.order_key AS order_key,
SSOv2.order_number AS order_number,
SSOv2.order_id AS order_id,
SSOv2.order_date AS order_date,
SSOv2.storename AS storename,
SSOv2.order_total AS order_total,
SSOv2.qtyshipped AS qtyshipped,
SSOv2.shippingpaid AS shippingpaid,
SSOv2.name AS name,
SSOv2.company AS company
FROM
shipstation_orders_v2 SSOv2
JOIN order_keys
ON SSOv2.order_id = OK.order_key
JOIN shipstation_fulfillments SSF
ON SSOv2.order_number = SSF.order_number ) t
) w
接下来,在没有看到对解决查询至关重要的实际数据或列出的结构的情况下,我将要求您编辑现有的 post。创建示例 table 列表 table、列和示例数据,以便我们可以查看您正在聚合和尝试从查询中获取的内容的基础。特别显示每个订单和履行可能有多行的地方,以及您期望结果显示的示例答案。