I'm having trouble using the LIKE operator in one of my cfqueries. Here's some background info. I have two tables, namely, Projects and Employees, like so:
[tt]
PROJECTS Table
index title employee_id
1 Build DB 102
2 Create Graphics 101,102,104
3 Write Code 102,103
4 Test Program 103
EMPLOYEES Table
id first last
101 John Lennon
102 Paul Mccartney
103 George Harrison
104 Richard Starkey
[/tt]
projects.employee_id must be a (comma-delimited) string data type AND
employees.id must be a number data type
Now here's the problem:
I have a search page that lets a user enter an employee_id, and the results page will display all projects that the employee is working on. If 0 is entered, it will output ALL employees. Here's my code:
[tt]
<cfquery name="results" datasource="ds" dbtype="ODBC">
SELECT projects.index,projects.title,projects.employee_id,employees.id,employees.first,employees.last
FROM projects,employees
WHERE 0 = 0
<cfif form.employee_id EQ 0>
AND projects.employee_id LIKE '%#form.employee_id#%'
</cfif>
AND employees.id LIKE projects.employee_id
ORDER BY employees.first
</cfquery>
<cfoutput query="results">
#results.first# is working on #results.title#.<br>
</cfoutput>
[/tt]
When I try to search, I only get records that doesn't contain a comma in the employee_id column. For example, if I search for all employees, I only get Projects 1 and 4; if I search for employee 102, I only get Project 1. I tried the IN operator instead of LIKE, but I got a data type mismatch error. I've also tried wildcards, subqueries and other operators, but still didn't work.
Any suggestion would be helpful. Thanks!
minsan
[tt]
PROJECTS Table
index title employee_id
1 Build DB 102
2 Create Graphics 101,102,104
3 Write Code 102,103
4 Test Program 103
EMPLOYEES Table
id first last
101 John Lennon
102 Paul Mccartney
103 George Harrison
104 Richard Starkey
[/tt]
projects.employee_id must be a (comma-delimited) string data type AND
employees.id must be a number data type
Now here's the problem:
I have a search page that lets a user enter an employee_id, and the results page will display all projects that the employee is working on. If 0 is entered, it will output ALL employees. Here's my code:
[tt]
<cfquery name="results" datasource="ds" dbtype="ODBC">
SELECT projects.index,projects.title,projects.employee_id,employees.id,employees.first,employees.last
FROM projects,employees
WHERE 0 = 0
<cfif form.employee_id EQ 0>
AND projects.employee_id LIKE '%#form.employee_id#%'
</cfif>
AND employees.id LIKE projects.employee_id
ORDER BY employees.first
</cfquery>
<cfoutput query="results">
#results.first# is working on #results.title#.<br>
</cfoutput>
[/tt]
When I try to search, I only get records that doesn't contain a comma in the employee_id column. For example, if I search for all employees, I only get Projects 1 and 4; if I search for employee 102, I only get Project 1. I tried the IN operator instead of LIKE, but I got a data type mismatch error. I've also tried wildcards, subqueries and other operators, but still didn't work.
Any suggestion would be helpful. Thanks!
minsan