Groups | Blog | Home
all groups > sql server (alternate) > march 2004 >

sql server (alternate) : Querying one column for two different data?


J Belly
3/31/2004 11:00:12 AM
Hi, all:

I've got a table listing height measurements and their primary keys. For
example:

HeightTable:

HeightID Height
-----
1................5'5"
2................5'6"
3................5'7"
and so on...

In another (linking) table, I've got a listing of people with height range
preferences. For example:

Members2Height:

MemberID HeightID_min HeightID_max
-----
1....................1.........................3
2....................1.........................2
and so on...

Now, if I want to display the results of what Member1 is looking for, is
there a way to query the Height table only once using the HeightID_min
and HeightID_max data, and displaying the results of that query?
For example, the only solution that I could think of was creating two
separate queries:

Query 1 (for min height):
-----
select h.Height
from HeightTable h, Members2Height m2h
where MemberID = cookie.MemberID
AND m2h.HeightID_min = h.HeightID

Query 2 (for max height):
------
select h.Height
from HeightTable h, Members2Height m2h
where MemberID = cookie.MemberID
AND m2h.HeightID_max = h.HeightID


I hope that made sense. Thanks for your help.

J




Lyndon Hills
3/31/2004 8:16:30 PM
[quoted text, click to view]
select h.height minimum, h1.height maximum
from members2height m, heighttable h, heighttable h1
where m.memberID = cookie.MemberID
and h.heightid=m.heightid_min
David Portas
3/31/2004 8:23:27 PM
SELECT A.height AS min_height,
B.height AS max_height
FROM Members2Height AS M
JOIN HeightTable AS A
ON M.heightid_min = A.heightid
JOIN HeightTable AS B
ON M.heightid_max = B.heightid
WHERE M.memberid = ?

But this seems needlessly complex to me. Why not just store the preferred
height values in the Members table with appropriate CHECK constraints?

CREATE TABLE Members (memberid INTEGER PRIMARY KEY, pref_min_height INTEGER
NOT NULL CHECK (pref_min_height BETWEEN 48 AND 84 /* inches */),
pref_max_height INTEGER NOT NULL CHECK (pref_max_height BETWEEN 48 AND 84),
CHECK (pref_min_height<=pref_max_height))

--
David Portas
SQL Server MVP
--

J Belly
4/1/2004 2:32:40 AM
Thank you both for your help. The column aliases worked out perfectly.

J



AddThis Social Bookmark Button