3

I have the following table:

userid    title         content
1         gender        male
1         location      NY    
2         gender        female
2         location      SF
3         gender        female
3         location      NY

I'm trying to retrieve only userid with gender="male" and location="NY"

if I try:

select userid
from table
where content="male"
   AND location="NY";

It will return null. Any idea how to do that?

3
  • BTW: This is a bad idea for a table structure. If at all possible I'd suggest rearchitecting it. Commented Nov 30, 2011 at 2:55
  • I know but it was designed this way some times ago. Commented Nov 30, 2011 at 3:02
  • I know what it's like to get stuck with legacy code. Commented Nov 30, 2011 at 3:03

1 Answer 1

1
SELECT t.userId
FROM yourTable AS t
INNER JOIN yourTable AS t2 ON t.userId = t2.userId
   AND t.title = 'gender'
   AND t.content = 'male'
   AND t2.title = 'location'
   AND t2.content = 'NY'

I would also consider looking at normalizing your data. It would make queries like this much easier (and probably faster) in the future.

Sign up to request clarification or add additional context in comments.

2 Comments

beat me by 35 seconds. Hate when that happens. Need to learn to type faster on these softball questions. +1
@JohnFx I definitely know how that goes, I've been too slow on many questions already tonight.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.