仅显示卡付款

时间:2019-09-19 13:13:53

标签: mysql sql

大约6年前,我设计了一个耕作系统,尽管代码令人沮丧且令人恐惧,但它仍在继续,我为其设计的咖啡馆一直在使用它。

但是,他们最近购买了一个刷卡机,所以现在当他们想查看他们拥有的那一天的所有销售额时,可以在两个单独的表中查看现金和信用卡。

这被证明是棘手的,并且它的SQL困扰了我-我无法理解。

以下是涉及的表:

类别

.photo-gallery {
  font-weight: bold;
}

.other {
  font-style: italic;
}

产品

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main id="Master">
  <div class="photo-gallery-RID459852 other any some">
    Algo
  </div>
  <div class="photo-gallery-RID987410 other any some2 other2">RID987410</div>
  <div>
    <div>
      <div class="photo-gallery-369841 other any some">369841</div>
    </div>
  </div>
  <article>
    <div class="photo-gallery-RID36541 here now other any some">RID36541</div>
  </article>
</main>

收据

+----+-------------------+---------+------------+
| id |       name        | display |     ts     |
+----+-------------------+---------+------------+
|  1 | Drinks            |       1 | 2016-10-14 |
|  2 | General Snacks    |       1 | 2016-10-14 |
|  3 | Lunch Options     |       1 | 2016-10-14 |
| 4  | Conference Drinks |       1 | 2016-10-14 |
+----+-------------------+---------+------------+

销售

+----+-----------------+-------+------+-----+---------+------------+
| id |      name       | price | cost | cID | display |     ts     |
+----+-----------------+-------+------+-----+---------+------------+
|  1 | English Tea     |   0.6 | 0.09 |   1 |       1 | 2018-02-15 |
|  2 | Speciality Teas |   0.8 | 0.17 |   1 |       1 | 2018-02-15 |
|  3 | Crisps          |   0.6 | 0.41 |   3 |       1 | 2018-02-15 |
|  4 | Chocolate Bar   |   0.6 |  0.5 |   3 |       1 | 2018-02-15 |
+----+-----------------+-------+------+-----+---------+------------+

请记住,我是很久以前写的,我知道它并不完美。只是为了解释上述内容,+----+-----+-----+----------+------------+ | id | oID | pID | quantity | ts | +----+-----+-----+----------+------------+ | 1 | 1 | 26 | 1 | 2013-11-21 | | 2 | 2 | 6 | 2 | 2013-11-21 | | 3 | 3 | 2 | 1 | 2013-11-21 | | 4 | 4 | 3 | 1 | 2013-11-21 | +----+-----+-----+----------+------------+ 代表+----+-------+----------+------+------+--------+------------+ | id | total | tendered | flag | card | userID | ts | +----+-------+----------+------+------+--------+------------+ | 1 | 1 | 1 | 0 | 0 | 4 | 2013-11-21 | | 2 | 2 | 2 | 0 | 0 | 4 | 2013-11-21 | | 3 | 0.6 | 0.6 | 0 | 0 | 4 | 2013-11-21 | | 4 | 0.6 | 0.6 | 0 | 0 | 4 | 2013-11-21 | +----+-------+----------+------+------+--------+------------+ ,但实际上应该是oID并链接到销售表ID,而orderID代表salesID且是链接到pID表的外键。同样,productID确实是products

好吧,咖啡厅经理要求提供一个类似于以下表格的表格:

cID

还好,我已经使用以下SQL语句来生成此代码:

categoryID

但是,现在我需要显示两个表格,一个用于卡位,另一个用于现金。现在,+---------------+-----+------+-------+------------+-----------+ | Drinks | Qty | Cost | Price | Cost-Total | Qty-total | +---------------+-----+------+-------+------------+-----------+ | Juice Carton | 2 | 33p | 60p | 66p | £1.20 | | Filter Coffee | 11 | 20p | 80p | £2.20 | £8.80 | | Sub Total | 13 | | | £2.86 | £10.00 | | Grand Total | 13 | | | £2.86 | £10.00 | +---------------+-----+------+-------+------------+-----------+ 表中的卡付款在卡列中带有SELECT categories.name AS category, products.name, pID, (SELECT SUM(quantity) FROM receipts s WHERE s.pID = r.pID AND DATE(s.ts) = CURDATE()) AS quantity, products.price, products.cost FROM receipts r LEFT JOIN products ON r.pID = products.id LEFT JOIN categories ON products.cID = categories.id WHERE DATE(r.ts) = CURDATE() GROUP BY r.pID ORDER BY categories.name; 标记。所以我试着这样写:

sales

但是,它只显示与第一个查询相同的数据。我知道问题出在哪里-嵌入的SELECT语句(1中),因为我没有在其中指定是卡付款还是现金付款。我以为只要在末尾加上SELECT categories.name AS category, products.name, pID, (SELECT SUM(quantity) FROM receipts s WHERE s.pID = r.pID AND DATE(s.ts) = CURDATE()) AS quantity, products.price, products.cost FROM receipts r LEFT JOIN products ON r.pID = products.id LEFT JOIN sales x on r.oID = x.id LEFT JOIN categories ON products.cID = categories.id WHERE DATE(r.ts) = CURDATE() AND x.card = 1 GROUP BY r.pID ORDER BY categories.name; 就可以了,但显然不行。

有人可以帮助我解决此SQL问题吗?我如何才能将卡条件放入嵌入式SQL中,因为它是从收据中检索的,而收据不包含有关是否进行卡付款的信息?

我真的不知道该如何进行。所有帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

基本形式:

SELECT * FROM Sales WHERE card = 1

仅显示信用卡付款

这将从今天开始为您带来任何销售:

SELECT *
  FROM 
    categories,
    products,
    sales,
    reciepts,
  LEFT JOIN
    products ON reciepts.pID
    sales ON reciepts.oID
    categories ON products.cID
  WHERE DATE(s.ts) = CURDATE() 
    AND sales.card = 1

`

所以您所拥有的是正确的。这是您想念的另一件事...

到目前为止,您还没有选择可能成为您问题的销售表。
您是否尝试过在SQL Management Studio中运行代码并查看结果是什么?

相关问题