X
X
XenK2016-07-21 10:37:29
PHP
XenK, 2016-07-21 10:37:29

How to combine a request?

There is a request like this:

SELECT sum(money) FROM `accounts` WHERE date >= CURDATE() AND tel_id = 1234 AND type = 1 AND status = 1

And like this:
SELECT sum(money) FROM `accounts` WHERE date >= CURDATE() AND tel_id IN (1542, 6624, 2524) AND type = 1 AND status = 1

How to combine these queries so that the result is:
--------------------------
| tel_id | sum1 | sum2 |
--------------------------

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-07-21
@XenK

SELECT T1.tel_id, T1.sum1, T2.sum2
FROM
  (SELECT tel_id, sum(money) AS sum1
   FROM accounts
   WHERE date >= CURDATE() AND tel_id = 1234 AND type = 1 AND status = 1
   ) T1,
  (SELECT tel_id, sum(money) AS sum2
   FROM accounts
   WHERE date >= CURDATE() AND tel_id IN (1542, 6624, 2524) AND type = 1 AND status = 1
  ) T2
WHERE T2.tel_id = T1.tel_id

T
ThunderCat, 2016-07-21
@ThunderCat

You don't need to merge anything, joining tables on itself is bad practice and slow queries. It makes no sense, there will be no gain in speed, and the logic of the request through the fifth point. Make 2 requests and combine data in php, then everything will work quickly and logically. Use KISS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question