简体   繁体   English

使用联接和嵌套查询计算数量

[英]Counting quantities using join and nested queries

I'm attempting to count stock quantities from active sales orders, which are stored in xcart_order_details. 我正在尝试计算有效销售订单中的库存数量,这些订单存储在xcart_order_details中。 I also want to only count stock from orders placed within x days which have a status of processed 'P' or queued 'Q', and also if the stock type matches certain locations; 我还只想统计x天内发出的状态为“ P”或“ Q”排队的订单中的库存,以及库存类型是否与特定位置匹配; C1 stock, C2 stock, and so on. C1库存,C2库存,等等。

This used to be relatively simple in our old xcart database; 在我们旧的xcart数据库中,这过去相对简单。

SELECT COUNT(`amount`)
FROM `xcart_order_details`
WHERE `productid` IN (
  SELECT `productid`
  FROM `xcart_products` WHERE `orderid` IN
    (SELECT `orderid`
     FROM `xcart_orders`
     WHERE `date` > ".$date_range."
       AND (`status` = 'P'
            OR `status` = 'Q'))
  AND (LOWER(param01) = 'c1 stock'
       OR LOWER(param01) = 'c2 stock'
       OR LOWER(param01) = 'g stock'
       OR LOWER(param01) = 'stock')
  AND `productid` = ".$safe_prodid.")

This query worked. 该查询有效。 But now our stock locations are stored in another table called xcart_extra_field_values, and must be retrieved where fieldid = 5; 但是现在我们的库存位置存储在另一个名为xcart_extra_field_values的表中,并且必须在fieldid = 5的情况下进行检索;

I've tried using a join to get the value field from xcart_extra_field_values where the fieldid = 5, and attempt to do what I'm doing with the query above, but it doesn't work. 我尝试使用联接从xcart_extra_field_values中获取fieldfield = 5的value字段,并尝试执行我在上述查询中所做的事情,但是它不起作用。

SELECT COUNT(`a.amount`)
FROM xcart_order_details a,
     xcart_extra_field_values b
WHERE a.productid IN (
  SELECT productid
  FROM xcart_products WHERE orderid IN
    (SELECT orderid
     FROM xcart_orders
     WHERE date > 1409529600
       AND (status = 'P'
            OR status = 'Q'))
  AND (LOWER(b.value) = 'c1 stock'
       OR LOWER(b.value) = 'c2 stock'
       OR LOWER(b.value) = 'g stock'
       OR LOWER(b.value) = 'stock')
  AND (a.productid = b.productid)
  AND (a.productid = 4169)
  AND (b.fieldid = 5)

Unfortunately this query does not work, and I know it's a syntax issue, but the error I'm getting is no help whatsoever. 不幸的是,该查询不起作用,我知道这是一个语法问题,但是我得到的错误无济于事。

Can anyone shed any light on what I'm doing wrong? 谁能告诉我我在做什么错?

You are missing the criteria how to join the two tables. 您缺少如何连接两个表的条​​件。 Let's assume there is an xcart_order_details_id in table xcart_extra_field_values, then use that. 假设表xcart_extra_field_values中有一个xcart_order_details_id,然后使用它。

After all, you don't even have to join. 毕竟,您甚至不必加入。 You want results from xcart_order_details for which certain detail value records EXIST: 您需要xcart_order_details的结果,某些详细值记录为EXIST:

SELECT COUNT(`amount`)
FROM `xcart_order_details`
WHERE `productid` IN 
(
  SELECT `productid`
  FROM `xcart_products` 
  WHERE `orderid` IN
  (
    SELECT `orderid`
    FROM `xcart_orders`
    WHERE `date` > ".$date_range."
    AND `status` IN ('P','Q')
  )
AND `productid` = ".$safe_prodid."
AND EXISTS
(
  SELECT *
  FROM xcart_extra_field_values v
  WHERE v.xcart_order_details_id = xcart_order_details.id
  AND fieldid = 5
  AND LOWER(param01) IN ('c1 stock','c2 stock','g stock','stock')
);

BTW: You count how many order detail records have an amount? 顺便说一句:您数多少个订单明细记录?

Resolved. 解决。

SELECT COUNT(a.amount)
FROM xcart_order_details a,
     xcart_extra_field_values b
WHERE a.productid IN
    (SELECT productid
     FROM xcart_products
     WHERE orderid IN
         (SELECT orderid
          FROM xcart_orders
          WHERE date > 1409529600
            AND (status = 'P'
                 OR status = 'Q'))
       AND (LOWER(b.value) = 'c1 stock'
            OR LOWER(b.value) = 'c2 stock'
            OR LOWER(b.value) = 'g stock'
            OR LOWER(b.value) = 'stock'))
       AND (a.productid = b.productid)
       AND (a.productid = 4169)
       AND (b.fieldid = 5)

Firstly, I was missing a bracket at the end of my query as I was nesting two IN queries, and I needed to remove the Grave accents around a.amount within COUNT, which it didn't like. 首先,当我嵌套两个IN查询时,我在查询末尾缺少括号,并且我需要删除COUNT中a.amount附近的Grave口音,这是不希望的。 I only noticed the missing bracket when I looked at my queries side by side. 当我并排查看查询时,我只注意到缺少的括号。

Edit: Moved my join outside of my nested IN clause, as it made no sense to have it nested. 编辑:将我的联接移到我的嵌套IN子句之外,因为嵌套它没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM