homepage Welcome to WebmasterWorld Guest from 23.20.61.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
best way to check a record has certain entries in a field
lethal0r

5+ Year Member



 
Msg#: 3762836 posted 11:52 am on Oct 10, 2008 (gmt 0)

I need to check if a widget exists in a table with both a widget_attribute_id of 1 AND a widget_attribute_id of 2.

what is the best way to do so?

at the moment I am doing

SELECT * FROM (
SELECT COUNT(DISTINCT widget_attribute_id) AS widget_attribute_count FROM widgets WHERE (widget_attribute_id = 1 OR widget_attribute_id = 2))
AS matching_attributes WHERE widget_attribute_id > 1

 

mark_roach

10+ Year Member



 
Msg#: 3762836 posted 2:46 pm on Oct 10, 2008 (gmt 0)

Join the table with itself.

Select a.widget from widget_table a, widget_table b where a.widget_attribute_id=1 and b.widget_attribute_id=2 and a.widget=b.widget

lethal0r

5+ Year Member



 
Msg#: 3762836 posted 12:34 pm on Oct 11, 2008 (gmt 0)

thanks thats very handy.

can you also do that where the relationship a.widget=b.widget is more complex?

eg where the records would be joined using other tables.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved