Quantcast
Channel: User Mohamed Mufeed - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Answer by Mohamed Mufeed for MariaDB SQL Query inner join table messages with table users

$
0
0

You should join the users table twice.

And I would go for LEFT JOIN rather than INNER JOIN as it is more appropriate.

SELECT     messages.id,    u1.username as name_from,    u2.username as name_to,    messages.contentFROM messages    LEFT JOIN users u1         ON messages.id_from = u1.id    LEFT JOIN users u2        ON messages.id_to = u2.id

The key here is that you join the tables with forign keys from messages table and their corresponding primary key from users table


Viewing all articles
Browse latest Browse all 36

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>