I have a table called faq. This table consists from fields faq_id,faq_subject.
I have another table called article which consists of article_id,ticket_id,a_body and which stores articles in a specific ticket. Naturally there is also a table “ticket” with fields ticket_id,ticket_number.
I want to retrieve a result table in format:
ticket_number,faq_id,faq_subject.
In order to do this I need to search for faq_id in the article.a_body field using %LIKE% statement.
My question is, how can I do this dynamically such that I return with SQL one result table, which is in format ticket_number,faq_id,faq_subject.
I tried multiple configurations of UNION ALL, LEFT JOIN, LEFT OUTER JOIN statements, but they all return either too many rows, or have different problems.
Is this even possible with MySQL, and is it possible to write an SQL statement which includes @variables and can take care of this?
First off, that kind of a design is problematic. You have certain data embedded within another column, which is going to cause logic as well as performance problems (since you can’t index the
a_bodyin such a way that it will help theJOIN). If this is a one-time thing then that’s one issue, but otherwise you’re going to have problems with this design.Second, consider this example: You’re searching for
faq_id#123. You have an article that includesfaq_id4123. You’re going to end up with a false match there. You can embed thefaq_idvalues in the text with some sort of mark-up (for example,[faq_id:123]), but at that point you might as well be saving them off in another table as well.The following query should work (I think that MySQL supports
CAST, if not then you might need to adjust that).EDIT: Corrected to use
CONCAT