简体   繁体   English

MYSQL连接查询无法按预期工作

[英]MYSQL join query not working as expected

I have basically have two tables: 我基本上有两个表:

1) billingcharges : here we store id of a restaurant (restid) , charge id (chargeid), chargetime(timeinmillis when the charge occurred),chargeamount(int amount of the actual charge).charge id is a foreign key to the billingchargedetails table. 1)billingcharges:在这里我们存储了一个餐厅的id(restid),charge id(chargeid),chargetime(发生收费的时间),chargeamount(实际费用的整数).charge id是billingchargedetails表的外键。

2) billingchargedetails: here we store the details of all the possible charges. 2)billingchargedetails:在这里,我们存储所有可能收费的详细信息。 chargeid(primary key int), chargename (name of the charge), perdaycost (cost per day of the charge) chargeid(主键int),chargename(费用名称),perdaycost(费用每天的费用)

What i expect: 我期望的是:

a summary report of totalamount of charge for each charge for each restaurant. 每个餐厅每笔费用的总费用摘要报告。

The current entries inside the tables are: 表中的当前条目为:

select * from billingcharges;
+--------+----------+---------------+--------------+
| restid | chargeid | chargetime    | chargeamount |
+--------+----------+---------------+--------------+
|      1 |        1 | 1536363636363 |          700 |
|      2 |        1 | 1536363636363 |          500 |
|      1 |        1 | 1568789654123 |          500 |
+--------+----------+---------------+--------------+

select * from billingchargedetails;

+----------+--------------------+------------------+
| chargeid | chargename         | chargecostperday |
+----------+--------------------+------------------+
|        1 | Base Charge        |               50 |
|        2 | Spotlight Listing  |               50 |
|        3 | Gold Notification  |              500 |
|        4 | Discount (FIRST50) |               18 |
+----------+--------------------+------------------+

A simple join on chargeid ended up not giving me the qty and sum as expected.so i need some form of a left or right outer join, that much i know and tried 一个对Chargeid的简单连接最终没有给我期望的数量和总和。因此,我需要某种形式的左右外部连接,这是我所知道并尝试过的

I tried a left join as follows: 我尝试了如下左联接:

select restid, B.chargeid, chargename, count(B.chargeid) as qty,
  sum(ifnull(chargeamount,0)) as total 
from billingcharges as B 
left join billingchargedetails as C on B.chargeid=C.chargeid 
group by restid,B.chargeid;

+--------+----------+-------------+-----+-------+
| restid | chargeid | chargename  | qty | total |
+--------+----------+-------------+-----+-------+
|      1 |        1 | Base Charge |   2 |  1200 |
|      2 |        1 | Base Charge |   1 |   500 |
+--------+----------+-------------+-----+-------+

This does work and sums things but there are missing charges for each restaurant. 这确实可以工作并求和,但是每家餐厅都缺少收费。 even if they arent present inside the billinghcarges ie the left table, i need it with qty 0 and total 0. 即使他们没有出现在billinghcarges内部,即左表,我也需要qty 0和total 0。

I tried a right join and a random value was selected by mysql from the non existing entries inside the left table as follows: 我尝试了一个右连接,并且mysql从左表中的不存在的条目中选择了一个随机值,如下所示:

select restid, B.chargeid, chargename, count(B.chargeid) as qty,
  sum(ifnull(chargeamount,0)) as total 
from billingcharges as B 
right join billingchargedetails as C on B.chargeid=C.chargeid 
group by restid,B.chargeid;

+--------+----------+-------------------+-----+-------+
| restid | chargeid | chargename        | qty | total |
+--------+----------+-------------------+-----+-------+
|   NULL |     NULL | Spotlight Listing |   0 |     0 |
|      1 |        1 | Base Charge       |   2 |  1200 |
|      2 |        1 | Base Charge       |   1 |   500 |
+--------+----------+-------------------+-----+-------+

The expected output is something like: 预期的输出如下所示:

 restid chargeid  chargename   qty totalamount
  1        1      Base Charge   2   1200
  1        2      Spotlight     0   0
  1        3      Gold          0   0
  1        4      Discount      0   0
  2        1      Base Charge   1   500
  2        2      Spotlight     0   0
  2        3      Gold          0   0
  2        4      Discount      0   0
  'same as above expected for each restid in billingcharges'

Before you can do the outer join, you need to generate the cross-product of restaurants to charge types. 在进行外部联接之前,您需要生成餐厅的叉积以收取类型。

Something like the following (but I have not tested it): 类似于以下内容(但我尚未对其进行测试):

SELECT R.restid, D.chargename, COUNT(B.chargeid) AS qty,
  SUM(IFNULL(B.chargeamount, 0)) AS total
FROM (SELECT DISTINCT restid FROM billingcharges) AS R
CROSS JOIN billingchargedetails AS D
LEFT JOIN billingcharges AS B ON R.restid=B.restid AND D.chargeid=B.chargeid
GROUP BY R.restid, D.chargename;

In this example, the cross-product of R and D is every restaurant crossed with every charge type. 在此示例中,R和D的叉积是每种餐厅和每种收费类型的交叉。

Of course not all of those charges exist for every restaurant. 当然,并不是每个餐馆都存在所有这些费用。 So the outer join to billingcharges finds those rows that do exist for each respective combination of restaurant & charge type. 因此,外部连接到billingcharges会找到针对餐厅和费用类型的每种相应组合确实存在的行。

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

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