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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query newbie question

Author  Topic 

bernie
Starting Member

6 Posts

Posted - 2010-01-23 : 08:55:19
Hi all,
Im new to SQL and I'm a bit lost with a query. From a table like this:

*****************
url | feature
*****************
www.a.de | x
www.a.de | y
www.b.de | x
www.n.de | p
*****************

I would like to query all urls that have the features x and y (both). In this case, the result would be www.a.de

How can I do this? Thanks! bernie

proxteam
Starting Member

2 Posts

Posted - 2010-01-23 : 11:03:14
SELECT url
FROM table AS objTable
WHERE (feature = 'x')
AND EXISTS (SELECT feature FROM table WHERE (objTable.text = text) AND (feature = 'y'))

Greez proxteam
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-23 : 11:55:24
quote:
Originally posted by proxteam

SELECT url
FROM table AS objTable
WHERE (feature = 'x')
AND EXISTS (SELECT feature FROM table WHERE (objTable.url = url) AND (feature = 'y'))

Greez proxteam




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bernie
Starting Member

6 Posts

Posted - 2010-01-23 : 12:04:54
Thanks proxteam,
works! But a follow up: Could it be better to create a different table or view to improve performance of this kind of query?
Not sure if this question makes sense. It's just that the query seems a bit complicated to me. And I wonder if my table setup is maybe not the best for this purpose.

Thanks, b
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-23 : 12:14:38
Nothing wrong with a self referencing query like this.

Whether the design of your database is ideal is another question! Not enough information here to give you advice on that.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-23 : 12:14:58
There is a one to many relationship between url and feature.
It would be better to have a table for url and another table for the features.

for example:

url_table
id int
url varchar(500)

feature_table
id int
url_id
feature


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bernie
Starting Member

6 Posts

Posted - 2010-01-23 : 12:53:25
Ok, I get the impression I need to think about my database design.

It's actually a many-to-many relation. A url has many features and a feature can occur in many urls. And I will regularly have to query for all urls with features x,y,z,...

How would a good table setup look like for this?


Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-23 : 13:38:31
many to many needs a third table to link url_id with feature_id.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-24 : 04:43:50
Third table is definitely the "text book answer", but I think it depends a bit ....

The URLs make a good natural key (although they are quite long)

I don't know about the features. If they are simple words then they would be good natural keys too.

If you are going to store some other data about the URLs, or the Features (Active/Inactive, Long description, etc.) then definitely separate tables and a "third table" to link them. But if it is literally URL + FeatureName then I'm not so sure.

Even then, a separate table reduces the likelihood of the FeatureName being wrongly spelt, or inconsistently used.
Go to Top of Page

bernie
Starting Member

6 Posts

Posted - 2010-01-24 : 05:47:32
Ok, I will try to use URLs and features as keys. But I have to think and read a bit more on how to setup my tables. Maybe I will come back to that later, with more detailed info...

Thanks for your help!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-24 : 06:16:51
url_table
========
URL_ID int
URL varchar(500),
... other columns ...

feature_table
=============
Feature_ID int,
Name varchar(50),
... other columns ...

URL_FeatureLink table
=====================
URL_ID int,
Feature_ID int
Go to Top of Page
   

- Advertisement -