Skip to content
Advertisement

SQL writing format

I would like to inner join to tables with sql like this:

$check_unscored =   "select * from [user]

            INNER JOIN [tenderrc]
            on [user].[id] = [tenderrc].[userid]";

$result_unscored = mssql_query($check_unscored, $s);

while ($record_unscored = mssql_fetch_array($result_unscored))
{
$id = $record_unscored['id'];
$name = $record_unscored['name'];   
$userid = $record_unscored['userid'];   
$touser = $record_unscored['touser'];

$id = $record_unscored['id'];
$username = $record_unscored['username'];   
$password = $record_unscored['password'];
}

I observed that a key name column called “id” exists in both table, and therefore $id = $record_unscored[‘id’]; would be overwritten;

I tried $id = $record_unscored[‘user.id’]; but it’s not working! Actually how shall I specify the table name as well?

Advertisement

Answer

Use aliases:

$check_unscored =   "
       SELECT 
           u.id
         , u.name 
         , u.userid
         , u.touser
         , t.id          AS tender_id              --- column alias
                                                   --- to remove duplicate names
         , t.username 
         , t.password
        FROM [user]   AS u                         --- table alias her
            INNER JOIN [tenderrc]   AS t           --- and here
                ON  u.[id] = t.[userid]
                    ";
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement