p4rtiz4n 1 p4rtiz4n 1 - 1 month ago 10
MySQL Question

how to join 2 results for show in a result

Hi i have two query :

this is for select list :


select * from buy


Buy tables :

ID | product_id| Time


and i want to add text for every records, this is query for select text for every records

Updated


final result is something like this

{
"ID": "13",
"product_id": "4",
"Date": "2016-10-28 00:00:00",
{
"txt_1": "hi this is txt 1",
"txt_2": "this is txt 2"
}
}


for that result i use from this code :

$result = $con->executeNoneQuery($sql_1);//get all records
while($row=mysql_fetch_object($result)){
$result2 = $con->executeNoneQuery($sql_2);//get txt for every records
}


now i want to know can i get both result with one sql query ? or any better idea ?

something like this

{
"ID": "13",
"product_id": "4",
"Date": "2016-10-28 00:00:00",
"txt_1": "hi this is txt 1",
"txt_2": "this is txt 2"
}


Update :
Sorry I'm confused with real second query
secend query is this :

select txt from product_id,text_list where text_list.id=product_id.id and product_id='a1' order by rand() limit 2

Answer

You can use this query :

SELECT t2.*,GROUP_CONCAT(t.text SEPARATOR '###') as txt 
FROM buy t2 
LEFT JOIN txt t on t2.id = t.product_id
LEFT JOIN product_id p on t.id=p.id
where t.product_id='al' 
GROUP by t.product_id

And after that in your PHP code you try that :

$txts = explode("###", txt);

To get array of text fields for every row