Showing posts with label structurei. Show all posts
Showing posts with label structurei. Show all posts

Tuesday, March 20, 2012

problems building sql statement

Hello,

I'm having problems building an sql stament that joins a few tables. I can seem to get my head around the structure!

I have to try and link up four different tables to try and get my result.

Here are the 4 table structures...

Web_Users
------
User_ID
Name


Tags_Table
------
Tags_ID
User_ID
Group_ID
Title

Created_Groups
--------
Group_ID
Group_Name


Tags_To_Groups
--------
Group_Link_ID
Group_ID
Tag_ID

Basically, this database, has four tables; One table (Web_Users) that contains a users name, and assigns a unique ID (User_ID), another table that stores a users tags they have created, and also links it to a group_ID. The created_groups table, contains group names and assigns a unique id also. And the last table, Tags_To_Groups, links tags to groups.

So this is what I'm trying to do...

I'm trying to get the Group_name field from Created_Groups table, of a tag , that belongs to a certain user. If sounds easy when I say it like that, but I've been inner joining tables all night and failing every time.

Does this make sense? Can anyone help?

Thank you

Hmm... maybe I didn't understand, but here's what I came up with:

select cg.Group_Name
from Created_Groups cg (nolock)
join Tags_Table tt (nolock) on tt.Group_ID = cg.Group_ID
and tt.User_ID = @.User_ID

|||

select cg.Group_Name
from Created_Groups cg
inner join Tags_Table tt on cg.Group_ID = tt.Group_ID
and tt.User_ID = ?

or:

select cg.Group_Name
from Created_Groups cg
inner join Tags_Table tt on cg.Group_ID = tt.Group_ID
inner join Web_Users wu on tt.User_ID = wu.User_ID
and wu.User_Name = '?'
or

select cg.GroupName
from Web_Users wu
inner join Tags_Table tt on wu.User_ID = tt.User_ID
and wu.User_Name = '?'
inner join Created_Groups cg on tt.Group_ID = cg.Group_ID

Depending upon indexes, some ways will be faster than others.

|||

amazing. thank you both!Smile