By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

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

100+
P: 134
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..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $q1=mysql_query("SELECT a.*,b.name FROM category as b,subcategory as a WHERE a.sid=b.id");
  3. $r1=mysql_fetch_array($q1);
  4.  
  5. echo $r1['name'];
  6. ?>
  7.  
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
Apr 15 '10 #1

✓ answered by chathura86

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

Share this Question
Share on Google+
2 Replies


chathura86
100+
P: 227
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
Apr 15 '10 #2

100+
P: 134
ya thanks man.,

i didnt think of that option..

thank u..
Apr 15 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.