0

How can I join two tables? Where I can get the product name, product image and product desc in tblproduct, and product_stocks, product_price and product_size in tblproduct_extension?

Here is my code:

SELECT tblproduct.product_image
   , tblproduct.product_name
   , tblproduct_extension.product_price
   , tblproduct.product_desc
   , tblproduct_extension.product_stocks
INNER JOIN tblproduct_extension
ON tblproduct.id=tblproduct_extension.product_id;

Table: tblproduct_extension tblproduct

pirho
  • 11,565
  • 12
  • 43
  • 70
Yeezus
  • 57
  • 8

2 Answers2

0

You can use a LEFT JOIN so only products that match in the tblproduct will be selected:

SELECT * FROM `tblproduct`
    LEFT JOIN `tblproduct_extension`
    ON `tblproduct`.`id` = `tblproduct_extension`.`product_id`;

Edit

If you want to get it using ID (using your example):

//GET PRODUCT DETAILS
$id = $_GET['view_product'];

$search_query = "SELECT * FROM `tblproduct`
    LEFT JOIN `tblproduct_extension`
    ON `tblproduct`.`id` = `tblproduct_extension`.`product_id`
    WHERE `tblproduct`.`id` = ".$id."";
giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • What if I get the product information base on id? This is my code but it's not working. //GET PRODUCT DETAILS $id = $_GET['view_product']; $search_query = "SELECT * FROM tblproduct LEFT JOIN tblproduct_extension ON tblproduct.id=tblproduct_extension.id WHERE id='$id'"; $query = mysqli_query($db_conn, $search_query); – Yeezus Nov 02 '17 at 11:33
  • I've updated my answer, you just need to add a conditional `WHERE` clause at the end... – giolliano sulit Nov 02 '17 at 11:36
  • Thanks!! Godbless you. – Yeezus Nov 02 '17 at 11:39
  • Hey. How can I get the product size for each product? Pic: https://i.stack.imgur.com/u08eI.png $id = $_GET['view_product']; $search_query = "SELECT * FROM tblproduct LEFT JOIN tblproduct_extension ON tblproduct.id=tblproduct_extension.product_id WHERE tblproduct.id='$id'"; $query = mysqli_query($db_conn, $search_query); $row = mysqli_fetch_array($query); – Yeezus Nov 09 '17 at 14:55
0
SELECT fields
FROM table
INNER JOIN 
another_table ON 
condition
TarangP
  • 2,711
  • 5
  • 20
  • 41