0

我有两张桌子,filesservers。这是架构:

f_id, s_id, u_id, name, size, downloads, uploaded

s_id, name, description, disk_space, used_space, enabled

现在,我正在尝试获取有关特定文件以及它托管在哪个服务器上的一些信息。在files表中,这是由s_id列确定的,列是表的外键servers。我想检索文件名、大小、上传时间以及它所在的服务器。我试过这个:

public function get_file_details()
{
    $sql= 'SELECT name, size, uploaded, server
           FROM `files` f
           JOIN `servers` s
           ON f.s_id = s.u_id
           WHERE f_id = ?
           LIMIT 1';
}

但它不起作用。我究竟做错了什么?

4

2 回答 2

1
public function get_file_details()
{
    $sql= 'SELECT f.name, size, uploaded, s.name
           FROM `files` f
           JOIN `servers` s
           ON f.s_id = s.s_id
           WHERE f_id = ?
           LIMIT 1';
}

3 处更改:name => f.name,server => s.name,ON f.s_id = s.u_id => ON f.s_id = s.s_id

于 2012-06-03T22:33:44.837 回答
1

它显示此错误,因为两个表中都存在名称字段,因此它变得模棱两可。为避免此错误,请始终添加表名别名试试这个

SELECT f.name, f.size, f.uploaded, s.name
           FROM `files` f
           JOIN `servers` s ON f.s_id = s.s_id
           WHERE f_id = ?
           LIMIT 1'
于 2012-06-04T05:09:20.797 回答