Link to home
Start Free TrialLog in
Avatar of Randy Johnson
Randy JohnsonFlag for United States of America

asked on

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




Avatar of rob_lorentz
rob_lorentz

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>
Avatar of Randy Johnson

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of RCorfman
RCorfman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
having works like where, but works on the grouped records.