基于一列为第二个表中的多个值连接两个表

时间:2016-12-09 21:19:06

标签: php mysql json

我有这样的表store_profile

id Storename        Storetype        address
1  Samsung Store    Electronics      Delhi
2  Levi             Clothing         Mumbai
3  Soni             Electronics      Bangalore

和第二个表Offers

id Store_id         offer           price
1  Samsung Store    flat 30% off     20000
2  Levi             Exchange         5000
3  Samsung Store    Exchange offer   40000

我希望从store_profile获取特定类型的所有商店详细信息,比如电子产品,以及每个商店的商品详细信息,而不是重复,因为我在json中使用此语法。

Mysql查询是 - :

SELECT 
    *, Offers.Store_id
FROM 
    Store_profile 
INNER JOIN
    Offers ON Store_profile.Storename = Offers.Store_id 
           AND Store_profile.Storetype LIKE '%Electronics%'

这是我的PHP代码

$sql ="SELECT * , Offers.Store_id FROM Store_profile INNER JOIN Offers ON Store_profile.Storename = Offers.Store_id AND Store_profile.Storetype like '%Electronics%' LIMIT 0 , 300 ";
$r = mysqli_query($con,$sql);

$result = array();

 while($row = mysqli_fetch_array($r)){

   array_push($result,array(

        'Store_Name'=>$row['Storename'],
        'Store_address'=>$row['address'],
        'Offer_Title'=>$row['offer'],
        'MRP_Price'=>$row['price'],

 ));

}


echo json_encode(array('result'=>$result));

mysqli_close($con);

0 个答案:

没有答案