PHP: search in multiple tables at once?

How to make mysql query to check multiple tables at once? I mean, something like:

$sql = mysql_query("SELECT username FROM table1, table2, table3 WHERE username = '$username'"); $numer = mysql_num_rows($sql); echo "You have ".$number; 

Can you do that?

What I want to do is to show the user all his messages from the entire site.

+4
source share
3 answers

In the request that you specified, it uses a full outer join. Use union instead.

 SELECT username FROM table1 WHERE username = '$username' UNION SELECT username FROM table2 WHERE username = '$username' UNION SELECT username FROM table3 WHERE username = '$username' 
+3
source

You can do with UNION .

Change Select-Statement in your mysql_query to ...

 SELECT username FROM table1 WHERE username = '$username' UNION SELECT username FROM table2 WHERE username = '$username' UNION SELECT username FROM table3 WHERE username = '$username' 
+1
source

You can use the SQL union keyword for this . It is used to combine tables and obtain the desired data from them.

0
source

Source: https://habr.com/ru/post/1302657/


All Articles