我想使用这样的sql内连接到表: -
$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'];
}
我发现两个表中都存在名为“id”的键名列,因此$ id = $ record_unscored ['id'];会被覆盖;
我试过$ id = $ record_unscored ['user.id'];但它不起作用!实际上我该如何指定表名呢?
谢谢!
答案 0 :(得分:1)
您可以创建别名字段等 如果你不是用户的id,你可以使用这样的东西 选择[user]。[id]为'userid',[tenderrc]。[*] from ... 我来自mySql世界,但我认为它可以通过这种方式管理 然后以这种方式访问它 $ id = $ record_unscored ['userid'];
答案 1 :(得分:1)
使用别名:
$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]
";
答案 2 :(得分:0)
您可以使用此功能
执行此操作http://hu2.php.net/manual/en/function.mssql-fetch-field.php
可以提供“column_source - 从中获取列的表”。
示例代码:
$result = mssql_query("....");
$check_unscored = "
SELECT *
FROM [user]
INNER JOIN [tenderrc] ON [user].[id] = [tenderrc].[userid]";
$rows = fetch_multitable( mssql_query( $check_unscored, $s ));
foreach( $rows as $row ){
echo $row->user['id'];
echo $row->tenderrc['id'];
}
function fetch_multitable( $result ){
$fields = mssql_num_fields( $result );
$infos = array();
for( $i=0; $i < $fields; $i++ ){
$fieldinfo = mssql_fetch_field( $r, $i );
$infos[ $i ] = (object)array(
'table'=> $fieldinfo -> column_source
,'name' => $fieldinfo -> name
);
}
$rows = array();
while( $raw = mssql_fetch_row( $r )){
$row = array();
foreach( $raw as $i => $v ){
$info = $infos[ $i ];
$row[ $inf->table ][ $inf->name ] = $v;
}
$rows[] = (object)$row;
}
return $rows;
}