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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

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

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.

Very Important crosS Applying yaK Herder

52326 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

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000