A
A
Alexey Evlait2018-10-09 23:23:04
MySQL
Alexey Evlait, 2018-10-09 23:23:04

How to merge 2 database queries written in PHP into one?

Hello everyone and thank you for deciding to try to help the half-educated.
There are 3 tables (names are given through constants):
the 1st (TABLE_PRODUCTS_TO_PRODUCTS_EXTRA_FIELDS) contains the properties and id of the products for which they are set
+---------------+------- --------+-----------------------+----------- -----------------------+
|_____ID_______|_products_id_|_products_extra_fields_id|_products_extra_fields_value_|
+---------------+---------------+----------------- ------------+----------------------------------+
|______1______ |______6 ______|_____________4____________|______Y________________|
|______2 ______|______45_____|______4____________| _____________ Y ______________ |
2nd (TABLE_PRODUCTS) for short
+---------------+---------------+----------------- ------------+----------------------------------+
|_____ID_______ |_products_id_|_____products_image_____|____products_page_url_______|
+---------------+---------------+----------------- ------------+----------------------------------+
|______1_______ |______6_______|______photo1.jpg__________|__________product1.html________|
|______2_______|______25______|_____photo2.jpg__________|__________product2.html________|
3rd (TABLE_PRODUCTS_DESCRIPTION) abbreviated
+---------------+---------------+------------ --------+
|_____ID_______|_products_id_|_products_name_|
+---------------+---------------+----------------- ---+
|______1_______|______6_______|____Item 1______|
|______1_______|______25______|____Item 2______|
I make the selection as follows:
from the 1st table I select 5 id of goods, in which this property = 'Y'
The second I select the necessary properties of goods from 2 commodity tables.
How it looks in code:

$sql = "
    SELECT
        products_id
    FROM " . TABLE_PRODUCTS_TO_PRODUCTS_EXTRA_FIELDS . "
    WHERE
         products_extra_fields_id = '4'
         and products_extra_fields_value = 'Y'
    LIMIT 5";

$productsIds = "";

$query = vamDBquery($sql);

while ($one = vam_db_fetch_array($query,true)) {
    $productsIds .= $one['products_id'].',';    
}

// Теперь выбираем сами товары

$productsIds = substr($productsIds, 0, -1);

$sqlProd = "
    SELECT
        p.products_id,
        p.products_image,
        p.products_page_url,
        p.products_price,
        p.products_tax_class_id,
        d.products_name
    FROM " . TABLE_PRODUCTS . " AS p, " . TABLE_PRODUCTS_DESCRIPTION . " AS d
    WHERE
         p.products_id IN (".$productsIds.")
         and p.products_id=d.products_id";

$queryProd = vamDBquery($sqlProd);

Further, through fetch, this is all collected in one array and correctly displayed, i.e. it all works.
BUT!
I "spit" on the way that id-shniks are collected, and indeed from 2 requests for 1 action. Yes, in general, the code, it seems to me, cannot even be called a crutch ...
How can this be optimized? I would like to merge 2 requests together...
Thanks in advance for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nozzy, 2018-10-09
@JustFeeLin

select
t1.*,
t2.*,
t3.*
from
TABLE_PRODUCTS_TO_PRODUCTS_EXTRA_FIELDS t1
join TABLE_PRODUCTS t2 on t2.products_id = t1.products_id
join TABLE_PRODUCTS_DESCRIPTION t3 on t3.products_id = t1.products_id
where t1.products_extra_fields_id = '4'
and t1.products_extra_fields_value = 'Y'
limit 5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question