PHP mysql search multiple tables using a keyword

前端 未结 4 684
时光说笑
时光说笑 2020-11-28 04:01

I have three tables in my database which are:

messages
topics
comments

Each of these tables has two fields called \'content\' and \'title\'

4条回答
  •  渐次进展
    2020-11-28 05:04

    What you are probably looking for is the UNION command:

    SELECT id, 'messages' as 'table' FROM messages 
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    UNION
    SELECT id, 'topics' as 'table' FROM topics
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    UNION
    SELECT id, 'comments' as 'table' FROM comments
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    

提交回复
热议问题