我想从 2 个不同的 table 做连接查询,我想从两个 table 输出

I want to do join query from 2 different table and i want output from both table

我应用 LEFT JOINRIGHT JOIN 查询以从两个 table..

获取输出

有两个 table :

第一个 table for Product_list.. 这个 table 包含 category_id 这是外键和所有其他产品细节..

第二个 table for product_images.. 这个 table 包含 product_id 分别是外键和产品图像..

我想要特定产品的详细信息和产品图片 category_id..

我使用此查询但无法正常工作:

SELECT * 
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id =  '.$id.'
UNION 
SELECT * 
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id   

我得到这个输出:

API代码

<?php
error_reporting(0);

$response = array();

require_once __DIR__ . '/db_Connect.php';

// check for post data
if (isset($_GET["cat_id"])) {

    $id = $_GET['cat_id'];

    // get a product from products table
    //$q="SELECT * FROM product_list WHERE cat_id ='".$id."' ORDER BY pro_id DESC ";
    $q="SELECT * 
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id =  '.$id.'
UNION 
SELECT * 
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id WHERE product_list.cat_id =  '.$id.'";
    //print($q);
    $res = mysql_query($q);
    print($res);
    //exit();
    if (mysql_num_rows($res) > 0) {

        $responce["category_sub_list"]=array();

        // check for empty result
        while($result = mysql_fetch_array($res)) {

        $product=array();
        $product['pro_id']=$result['pro_id'];
        $product['cat_id']=$result['cat_id'];  
        $product['product_name']=$result['product_name'];
        $product['image']="http://friendzfashionz.com/pandora/admin/Sub_uploads/".$result['image'];
        $product['product_desc']=$result['product_desc'];

        array_push($responce["category_sub_list"],$product);
        } 
         $responce["success"]=1;
         echo json_encode($responce);

    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No user found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

请试试这个查询。您需要在两个查询中应用 where 条件。

SELECT * 
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id =  '.$id.'
UNION 
SELECT * 
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id WHERE product_list.cat_id =  '.$id.'