Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Save the whales. Collect the whole set.


devel / comp.databases.mysql / Re: Regd Merging These 2 MySql Queries into 1

SubjectAuthor
* Regd Merging These 2 MySql Queries into 1Paaro
`- Re: Regd Merging These 2 MySql Queries into 1John Levine

1
Regd Merging These 2 MySql Queries into 1

<8960622c-c6b0-4ad8-8fea-1e70aec930a1n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=88&group=comp.databases.mysql#88

  copy link   Newsgroups: comp.databases.mysql
X-Received: by 2002:ac8:570c:0:b0:2e1:ee0c:71c5 with SMTP id 12-20020ac8570c000000b002e1ee0c71c5mr20043069qtw.365.1649517843498;
Sat, 09 Apr 2022 08:24:03 -0700 (PDT)
X-Received: by 2002:a05:6214:c85:b0:441:2bb9:92fa with SMTP id
r5-20020a0562140c8500b004412bb992famr20191391qvr.21.1649517843353; Sat, 09
Apr 2022 08:24:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.databases.mysql
Date: Sat, 9 Apr 2022 08:24:02 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=49.205.132.240; posting-account=W3OKGwkAAABCaMQvOgyn4Mgom1Z7jy7b
NNTP-Posting-Host: 49.205.132.240
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8960622c-c6b0-4ad8-8fea-1e70aec930a1n@googlegroups.com>
Subject: Regd Merging These 2 MySql Queries into 1
From: paaroonl...@gmail.com (Paaro)
Injection-Date: Sat, 09 Apr 2022 15:24:03 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 19
 by: Paaro - Sat, 9 Apr 2022 15:24 UTC

Hi

I have following two SQL queries to execute in mySQL.

SELECT total(amount) as total_receipts FROM receipts WHERE torderid = 101
SELECT total(amount) as total_refunds FROM refunds WHERE torderid = 101

I get following below values using these two above queries

total_receipts
total_refunds

Now I need to calculate net_receipts as below.

net_receipts = total_receipts - total_refunds

Is there way to get total_receipts, total_refunds and net_receipts by merging the above two queries into a single SQL query?

Please help.

Re: Regd Merging These 2 MySql Queries into 1

<t2sfvu$2q8m$1@gal.iecc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=89&group=comp.databases.mysql#89

  copy link   Newsgroups: comp.databases.mysql
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!not-for-mail
From: joh...@taugh.com (John Levine)
Newsgroups: comp.databases.mysql
Subject: Re: Regd Merging These 2 MySql Queries into 1
Date: Sat, 9 Apr 2022 17:32:46 -0000 (UTC)
Organization: Taughannock Networks
Message-ID: <t2sfvu$2q8m$1@gal.iecc.com>
References: <8960622c-c6b0-4ad8-8fea-1e70aec930a1n@googlegroups.com>
Injection-Date: Sat, 9 Apr 2022 17:32:46 -0000 (UTC)
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970";
logging-data="92438"; mail-complaints-to="abuse@iecc.com"
In-Reply-To: <8960622c-c6b0-4ad8-8fea-1e70aec930a1n@googlegroups.com>
Cleverness: some
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: johnl@iecc.com (John Levine)
 by: John Levine - Sat, 9 Apr 2022 17:32 UTC

According to Paaro <paaroonline@gmail.com>:
>Hi
>
>I have following two SQL queries to execute in mySQL.
>
>SELECT total(amount) as total_receipts FROM receipts WHERE torderid = 101
>SELECT total(amount) as total_refunds FROM refunds WHERE torderid = 101
>
>I get following below values using these two above queries
>
>total_receipts
>total_refunds
>
>Now I need to calculate net_receipts as below.
>
>net_receipts = total_receipts - total_refunds
>
>Is there way to get total_receipts, total_refunds and net_receipts by merging the above two queries into a single SQL query?

It might be possible to do by abusing an outer join but I wouldn't recommend it.
If you want to get the three values in one result row it's easy to do with
a temporary table, e.g.:

CREATE TEMPORARY TABLE results (total_receipts decimal(5,2), total_refunds decimal(5,2)) ENGINE=MEMORY

INSERT INTO results(total_receipts) SELECT total(amount) FROM receipts WHERE torderid = 101
UPDATE results SET total_refunds= (SELECT total(amount) FROM refunds WHERE torderid = 101)
SELECT total_receipts, total_refunds, total_receipts-total_refunds AS net_receipts FROM results

If you do this very often you can put it into a procedure and make the 101 a parameter.

--
Regards,
John Levine, johnl@taugh.com, Primary Perpetrator of "The Internet for Dummies",
Please consider the environment before reading this e-mail. https://jl.ly

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor