PHP mysql search multiple tables using a keyword

前端 未结 4 691
时光说笑
时光说笑 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:01

    $query = "(SELECT content, title, 'msg' as type FROM messages WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'topic' as type FROM topics WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'comment' as type FROM comments WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%')";
    
    mysql_query($query);
    

    So, you are getting result from all of the three tables, and you can identify which row came from which table by looking at its type value.

提交回复
热议问题