I want to do a PDO request that will get informations from 2 different tables, but things fastly get hard, i explain :
I have a first database table (flowers) that is organized like this (but it’s REALLY bigger than what I show to you) :
ID; name; price of format 1; price of format 2; name of other format 3; price of other format 3;
FLOW0001; big yellow flower; ; 15,99; more big format; 34,99;
FLOW0002; little red flower; 5,99; 8,99; ; ;
… it goes on like this…
The second table (trees) is like this :
ID; name; name of format; price of format;
TREE0001; OMG BIG TREE !; F*** big format; 599,99;
TREE0001; OMG BIG TREE !; F*** even bigger format; 899,99;
TREE0002; litte ugly tree; little format; 20,99;
… it goes on like this…
The thing is that I want to “merge” these 2 tables and to show them togheter blended in a page like this :
while ($datas =$response->fetch()) //fetching the tables, they will be togheter and ordered by (their biggest) price. So, trees and flowers will blend.
{
// then echo the fetched datas. I will need to separate the prices and show them... some if/else based on pregmatches of the ID to know if it's an TREE or FLOW.
}
How to blend this ? Igot no Ideas. The worst is that I will need to do a page system (50 per pages). On the page system and in the pages, all the duplicated trees will need to appear as only uniques trees… HELP !
You can use a
unionto get the data from both tables. just be sure that you are returning matching columns in both queries. So if in your first query you have the following data typesint, text, varchar, decimal, decimalyou will need to make sure that the second query gets those same column types otherwise the DB will have a hissyfit about it. You can use an order by clause on the end to sort the data from both queries nicely.Something like this should do the trick:
Edit: You can
group_concat()fields in them if you want, and yes, you will be able to do anexplode()on them in your PHP. The default group_concat delimiter is a , (comma) so if you have commas in your fields, you will need to change it to something so that your explode works nicely.