Mysql
 sql >> Database >  >> RDS >> Mysql

PHP mysql cerca più tabelle usando una parola chiave

$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);

Quindi, stai ottenendo risultati da tutte e tre le tabelle e puoi identificare quale riga proviene da quale tabella guardando il suo type valore.