S
S
samorez7772018-06-01 12:22:40
MySQL
samorez777, 2018-06-01 12:22:40

Is it possible to combine 2 requests into 1?

Is it possible to combine 2 requests into one?

$products= \R::getAll("SELECT * FROM product_status JOIN product ON product.status_id = product_status.id JOIN pay ON product.paysystem_id = pay.pay_id");

and
$query = " SELECT product.*,(SUM(pay.summa)/product.price)*100 AS 'percent' FROM product JOIN pay ON (product.paysystem_id=pay.pay_id) GROUP BY product.id ";
$pay = \R::getAll($query);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2018-06-01
@DmitriyEntelis

Without knowing exactly the structure of tables and unique indexes, it is definitely impossible to answer.
In general, I do not advise doing this, requests overloaded by logic almost always turn out to be slower than 2 separate requests.

R
Ruslan., 2018-06-01
@LaRN

In MS SQL, this option works fine:

SELECT product_status.*,
             product.*,
             pay.*,
             ((SELECT SUM(pay.summa)
                 FROM pay
               WHERE pay.pay_id = product.paysystem_id)/product.price)*100 AS 'percent'
    FROM product_status 
      JOIN product 
        ON product.status_id = product_status.id 
      JOIN pay 
        ON pay.pay_id = product.paysystem_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question