How to fetch values from two tables with same fieldname using joint query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjayis
    New Member
    • Mar 2008
    • 134

    #1

    How to fetch values from two tables with same fieldname using joint query

    hi

    i have two tables which is named as category and subcategory ..

    category fieldnames

    1.id
    2.name
    3.status

    subcategory fieldnames

    1.id
    2.sid (category id)
    3.name
    4.status

    when displaying datas from table 2(subcategory)

    i wrote a joint query to fetch the category name from the sid present in the subcategory table..

    Code:
    <?php
    $q1=mysql_query("SELECT a.*,b.name FROM category as b,subcategory as a WHERE a.sid=b.id");
    $r1=mysql_fetch_array($q1);
    
    echo $r1['name'];
    ?>
    in the result ., when i displayed name it displays the category and not the sbucategory name.,

    how to get both the names using single query...

    thanks..



    regards
    vijay
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    change your sql to

    SELECT a.*,b.name as cat_name FROM category as b,subcategory as a WHERE a.sid=b.id

    so the common name will not be used

    Regards

    Comment

    • vjayis
      New Member
      • Mar 2008
      • 134

      #3
      ya thanks man.,

      i didnt think of that option..

      thank u..

      Comment

      Working...