Question:

Fetch data from two table Mysql Query?

by  |  earlier

0 LIKES UnLike

I have a table tbl_category with fields

ident,

CategoryName,

Description

and I have another table tbl_product with fields

ident,

ProductName,

CategoryId

Here CategoryId refers to the `tbl_category`.`ident` field!.

I want to fetch the all the ProductName and their corresponding CategoryName.

Someone Help me with query please!!

 Tags:

   Report

2 ANSWERS


  1. ans has the correct selects - which one you use is up to you. The first one is what is called an inner join - only those rows that fit the relation tbl_category.ident = tblProduct.CategoryId will be shown. The 2nd one is one type of an outer join - all rows from the tbl_category table are displayed regardless of whether there are any corresponding tbl_product rows, but if there are, then data from that table is shown as well. Judging from how you poised you question, you probably want the inner join in this case.


  2. SELECT p.ProductName,c.CategoryName FROM tbl_product as p,tbl_category as c WHERE p.CategoryId = c.ident

    OR

    SELECT p.ProductName,c.CategoryName FROM tbl_product as p LEFT JOIN tbl_category as c ON p.CategoryId = c.ident

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.