mysql row search

I have a table:

Table Keywords

id,keyword


Here is sample data

1,test1
2,test2
3,test2
4,test3
5,test4
1,test5
1,test6
2,test7
3,test8
2,test9


Let's say I want to get ID's where keyword is test5 and test6, but I want the ID number has to be the same.

ie if I did a search for test5 and test6 keywords it would return 1 because that is the only ID that matches the keywords

Any ideas how I would do this?

-Randy




LVL 6
rjohnsonjrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rob_lorentzCommented:
not sure what you are trying to accomplish, but you can select the distinct id from the table for your user's input. then check the number of records returned. something like this.....

<cfquery name="findRecs" datasource"yourDSN">
  select distinct id
  from yourTable
  where keyword like 'form.input'
</cfquery>
<cfif findRecs.recordCount gt 1>
    <cfset form.errorMsg = "keyword matched more than 1 result. refine your search">
</cfif>
rjohnsonjrAuthor Commented:
I want to do a search on the table and if I do a search on two keywords  test5 and test6 (from my example above)  if the ID is the same for both of the keywords found I want to return the ID.


Test5 and test6 both have the same ID in my table data above so the ID would be the same

Here is what I want

Select all IDs from keywords table where a row.keyword = test5 and a row.keyword = test6 and each row has the same ID.



-Randy
RCorfmanCommented:
One way to do this is with an Or query on the keywords and a group by having clause...
Something like this (I'm not a mysql expert, but I believe this syntax is supported by virtually every database (access, oractle, mysql, sql server, db2):

<cfquery name="commonids" datasource="yourdb">
select id from
table_keywords
where keyword = <cfqueryparam value="test5">
 or keyword=<cfqueryparam value="test6">
group by id
having count(*) > 1
</cfquery>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RCorfmanCommented:
You would substitute your form variables, or whatever for the test5 and test6....
where keyword = <cfqueryparam value="#trim(form.field1)#">
 or keyword=<cfqueryparam value="#trim(form.field2)#">

Building up the criteria in the where clause would be by whatver means is applicable.
The important point is to get all the entries for the keyword... group by id, then filter the list where the count(*) > 1... meaning that more than one record was found for that id that matched the keywords.
RCorfmanCommented:
having works like where, but works on the grouped records.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.