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.
| Author |
Topic |
|
Slither
Starting Member
3 Posts |
Posted - 2009-08-23 : 09:22:15
|
| I have the following code so far,SELECT item.name FROM items, item_variables WHERE items.variableRef = item_variables.itemVariableRef AND item_variables.variableId = 1 AND item_variables.variableId = 3;I only want to bring back the name of the item that has both the variable Id 1 and 3. How can I do this within one query. Currently instead of bring back one item that has 3 and 1 it brings back NO results presumably because it thinks I mean the same column.Thanks |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2009-08-23 : 12:18:08
|
| SELECT item.nameFROM itemsJOIN item_variables ON items.variableRef = item_variables.itemVariableRefWHERE item_variables.variableId IN (1,3) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-23 : 12:41:41
|
variableId cannot be 1 and also 3.You can use IN(1,3) like shown by singularity orWHERE items.variableRef = item_variables.itemVariableRefAND (item_variables.variableId = 1 OR item_variables.variableId = 3) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Slither
Starting Member
3 Posts |
Posted - 2009-08-23 : 15:01:35
|
| Ok thats not exactly what I wanted let me explain my table structure,Items table has a variable ref which links it to multiple categories.Item 1, itemVariables Ref (for instance number 1)Item_variables has data like.ItemVariable ref and a variableId so an Item has multiple categories1, 1,1, 3I want to bring back the items that have certain categories 1 and 3 from same table. That is why I am using an AND. I want to get items that have 1 and 3 as the category and not any others. Is there a way this can be done? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-23 : 16:18:52
|
[code]--Making tables and data for testdeclare @items table ([name] varchar(255), variableRef int)declare @items_variables table (itemVariableRef int, variableId int)insert @itemsselect 'name_1', 1 union allselect 'name_2', 2 union allselect 'name_3', 3insert @items_variablesselect 1,1 union allselect 1,3 union allselect 2,1 union allselect 2,2 union allselect 2,3 union allselect 3,1 union allselect 3,3-- select (DISTINCT) only names where categories 1,3 and not any othersselect DISTINCT [name] from @items ijoin @items_variables iv on i.variableRef = iv.itemVariableRef where not exists (select * from @items_variables where itemVariableRef = i.variableRef and (variableId <> 1 and variableId <> 3))[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-23 : 16:20:03
|
btw. giving tables, sample data and wanted output was your job  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Slither
Starting Member
3 Posts |
Posted - 2009-08-24 : 07:25:55
|
| Thanks webfred!That works perfectly and ive learned a little about subquery's in the process. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-24 : 08:07:35
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|