Search code examples
mysqldatabasestringwhere-clausesql-like

Is it possible to use alias as LIKE Search value?


Is it possible to use a MySQL alias as search string like so?

SELECT
    af.id
FROM
    things af
LEFT JOIN
    questions AS aq
ON
    aq.id = 25
LEFT JOIN
    countries AS ff
ON
    ff.item_id = af.id
LEFT JOIN
    categories AS cc
ON
    cc.id = aq.catid
WHERE
    af.published = 1 AND af.cat_id IN(2, 3) AND ff.field_id = 8 AND ff.value LIKE '%cc.title%'

Solution

  • AND ff.value LIKE '%cc.title%'

    MySQL will consider '%cc.title%' a litteral string, so your expression will not do what you want.

    Instead, you can use concat():

    AND ff.value LIKE CONCAT('%', cc.title, '%')