[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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




0
rjohnsonjr
Asked:
rjohnsonjr
  • 3
1 Solution
 
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>
0
 
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
0
 
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>
0
 
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.
0
 
RCorfmanCommented:
having works like where, but works on the grouped records.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now