Search code examples
mysqlsql-like

Working LIKE statement with % on column names but rows repeated


I have my SELECT query used with LIKE statement working but am shocked; that my rows fetched are repeated, and i don't know why?

SELECT *
FROM questions, counts
WHERE counts.test_coursecode LIKE '%'  || questions.coursecode || '%'

Solution

  • You must include the inner join of the two tables

    SELECT *
    FROM questions q inner join  counts c on a q.id and c.fk
    WHERE counts.test_coursecode LIKE CONCAT('%', questions.coursecode, '%')
    

    or

    SELECT *
    FROM questions q , counts c 
    where a q.id and c.fk
    and counts.test_coursecode LIKE CONCAT('%', questions.coursecode, '%')