【发布时间】:2018-09-11 16:10:10
【问题描述】:
Edit2:选择分离查询并在数据库输出之外将信息作为一个整体进行整理/处理。以 .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 列分布在三个表中。我在工作中使用它来组织相当不同的销售数据设置并理解它以识别趋势、季节性和所有这些好东西。我主要处理 Shipstation 数据。
我的问题是何时需要介绍第三张表。显然,有两个表只是一个简单的JOIN。我让它工作得很好。我在为第三张表正确设置 JOINs 时遇到了很多麻烦。
我正在尝试将来自两个最内层查询的数据JOIN 到shipstation_orders_v2 和order_keys 到我在第三个表中的shipstation_fulfillments 结果。
对于那些不使用 Shipstation 或不熟悉它的这个元素的人来说,履行与订单属于不同的类别,并且不使用完全相同的数据。这是我将它们粘合在一起的肮脏方式,因此我们有一些关于销售和运输趋势等的体面、可操作的信息。
我正在从shipstation_orders_v2 到order_keys 进行内部查询,作为SELECT DISTINCT 拆分订单总和的一种方式。在构建该子查询之前,我遇到了数据重复问题。使用(现在)子查询和子子查询,消除了重复问题,并且仅使用这两个表就可以正常工作。
问题是,当我将 SELECT 从 shipstation_fulfillments 和 JOIN 到子查询和子子查询时,我遇到了障碍。
我在处理这个查询时遇到了几个错误。按发生和解决的顺序:
Error 2013,在查询期间失去与服务器的连接(这告诉我我正在对三个连接的表进行全表读取,因为它没有事先出错,但我的 rinkadink 设置无法处理它)。我摆脱了那个。
然后,对于未识别的表名 shipstation_fulfillments,出现错误 1051。对我来说,我认为这可能是查询别名的问题。我不确定。
最后,好的 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`
【问题讨论】:
-
查询的主要部分中的“FROM”之前的最后一列是您的最后一列...删除它。
-
完成。编辑代码以反映这一点。在子查询
SELECT DISTINCT中仍然抛出错误1064。 -
什么是“WoT”?
-
文字墙。我相当罗嗦。
标签: mysql join mysql-error-1064