SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL 3 tables question
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

brigo
Starting Member

1 Posts

Posted - 02/25/2012 :  03:36:38  Show Profile  Reply with Quote
Hi everyone.
I have a question. I tried to make a page that would show info from 3 sql tables.
First table contains id and main_topic,
second table contains id id_main_topic and sub_topic,
and third table contains id, main_topic_id, sub_topic_id, and unit_information.
I try to do some thing like this

main_topic
sub_topic
unit_info
sub_topic
unit_info
unit info
unit info

What i mean, is some unit_info don't have a sub topic, so i try to attach them directly to main_topic. But i am stuck here, i can't get it working.

I wrote a $sql=("Select * From main_topic_table");
then inside of it a $sql2=("Select * FROM sub_topic where id_main_topic= $sql->id");
then inside of this i wrote one more
Select * FROM unit_info where sub_topic_id = $sql2 AND main_topic_id= $sql2->id_main_topic.

All this bring me unit_info only of those columns which have both main_topic_id and sub_topic_id, but not the ones that have only main_topic_id.
So i added to the last SELECT - OR (sub_topic_id=" " AND main_topic_id=$sql=id;

But all this still don't get me anywhere.
Could any one help me? Maybe there is another way to do that kind of stuff, could some one tell me?
If you need me to give more information i will do so. Please some one help me, i can't get it working for a week now.

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/25/2012 :  11:12:54  Show Profile  Reply with Quote
sounds like what you need is a recursive query which retrieves the whole hierarchy of topics.
Look for recursive cte approach below

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000