First of all, the query you want to build is not trivial because you are trying to get some results related to multiple rows. Therefore, I offer you the right solution in the form it should be (read: as the database developer will do this :-).
You must first modify the user_alias table so that it contains the id column, but not the name. It is not recommended to join your tables using the name field. The reason for this is that there may be two Sarah Connors.
Then you can get the results from both tables with this query:
SELECT user.*, user_alias.* FROM user LEFT JOIN user_alias ON user.id=user_alias.id
Thus, you will get the results in this format:
id | name | address | comments | user ------------------------------------------------------------- 1 | Sarah Connor | Planet Earth | Nice woman | sarah_connor 2 | Sarah Connor | USA, NY | Mean woman | sarah_c 3 | John Connor | USA, NY | n00b | john123
In situations where there are two or more entries (equal to id ) in the user_alias table for the same person, you will get something like this:
id | name | address | comments | user ------------------------------------------------------------- 4 | Bill Clinton | White House | President | bill 4 | Bill Clinton | White House | President | monica
Andrejs Cainikovs
source share