Search a DataTable with a foreign key reference
Search a DataTable with a foreign key reference
I have a AJAX server-side processing DataTable that has seven columns. Of these seven, four are string references to a foreign key. So in the example of a support system, I have the table tickets:
Tickets:
http://puu.sh/eDyXl/c60a7df07a.png [sorry, didn't know how to format it].
This table has several foreign references - for example ticket_priority references another table whereby 4 = Critical. Selecting data is fine, and the DataTable outputs 'Critical' as opposed to '4'. That's good, and what I need.
My headache, however, is how do I go about doing a search? If I was to search for 'Critical', the query would return null as the value in the table is actually '4'. I tried doing something along the lines of WHERE $search IN (SELECT .... )
but to no avail, and I'm now at a complete loss...!
The query I have at the moment is as follows...:
SELECT `ticket_id` AS DT_RowId, `ticket_date`, `ticket_title`,
(SELECT username FROM users WHERE tickets.user_id = users.id) AS username,
(SELECT status_value FROM ticket_status_id WHERE tickets.ticket_status = ticket_status_id.status_id) AS ticket_status,
(SELECT department_value FROM ticket_department_id WHERE tickets.ticket_department = ticket_department_id.department_id) AS ticket_department,
(SELECT priority_value FROM ticket_priority_id WHERE tickets.ticket_priority = ticket_priority_id.priority_id) AS ticket_priority
FROM (`tickets`)
WHERE `ticket_title` LIKE '%critical%'
ORDER BY `ticket_id` desc
LIMIT 10
(I've limited the WHERE to only the ticket_title so that I have at least some searching functionality).