Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL 3 tables question

Author  Topic 

brigo
Starting Member

1 Post

Posted - 2012-02-25 : 03:36:38
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

52326 Posts

Posted - 2012-02-25 : 11:12:54
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
   

- Advertisement -