F
F
FraudCrew2014-04-10 09:11:25
PHP
FraudCrew, 2014-04-10 09:11:25

Combine SQL 3 queries into 1 array

Subject, there are 3 requests like this:

$q1=mysql_query("SELECT id,price,group_id,name,description FROM `product` LIMIT 0, 200");
$q2=mysql_query("SELECT id,parent_id,name FROM `product_group` LIMIT 0, 200");
$q3=mysql_query("SELECT file FROM `product_image` ORDER BY `product_image`.`product_id` ASC LIMIT 0, 200");

How to combine into 1 array, so that it would be possible to display data from it within one cycle?
Played with UNION nothing comes out.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2014-04-10
@FraudCrew

select p.*, p_g.*, p_i.*
from `product` p
left join `product_group` p_g ON p_g.id = p.group_id
left join `product_image` p_i ON p_i.product_id = p.id
ORDER BY p_i.`product_id` ASC
LIMIT 0, 200

Here such request will allow to receive all data by one request in one array.
In SELECT, list the required fields so that there is no overhead

_
_ _, 2014-04-10
@AMar4enko

I wonder what your cycle, in this case, will do.

A
Alexander, 2014-04-10
@disc

Will data from product_group and product_image tables be pulled for products from the first query or not?
If so, then you need to use 2 JOINs here.

F
FraudCrew, 2014-04-10
@FraudCrew

@disc
@AMar4enko
Further, based on this data, I'm going to fill xml

$ymlcontent.="<offer id=\"".$row['id']."\" available=\"true\">\n";
                        $ymlcontent.="<url>http://www.site.ru/product/view/id/".$row['id']."</url>\n";
                        $ymlcontent.="<price>".$row['price']."</price>\n";
                        $ymlcontent.="<currencyId>RUR</currencyId>\n";
                        $ymlcontent.="<categoryId>".$row['group_id']."</categoryId>\n";

I just started learning php, I still don't understand many things.
Including what to do if there is an id column in two tables, how to call it later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question