Php mysql-获取min(主键)行的所有列

Php mysql-获取min(主键)行的所有列,php,mysql,sql,Php,Mysql,Sql,我需要将MIN(files.file\u id)行的所有列加入/添加到此查询,而不仅仅是将MIN(files.file\u id)的值作为第一个文件id返回 文件\u id是自动inc主键 表files包含文件id、用户id、文件名和时间戳列 我搜索了一下,似乎可以通过加入一个临时表来实现这一点,该临时表使用MIN(files.file\u id)从行中选择列,但无法获得任何工作 $stmt = $db->prepare(" SELECT accounts.acc

我需要将
MIN(files.file\u id)
行的所有列加入/添加到此查询,而不仅仅是将
MIN(files.file\u id)
的值作为
第一个文件id
返回

文件\u id
是自动inc主键

files
包含文件id、用户id、文件名和时间戳列

我搜索了一下,似乎可以通过加入一个临时表来实现这一点,该临时表使用
MIN(files.file\u id)
从行中选择列,但无法获得任何工作

$stmt = $db->prepare("
    SELECT 
        accounts.account_id,
        accounts.account_username,
        accounts.account_password,
        computers.computer_id,
        users.user_id,
        MIN(files.file_id) as first_file_id
    FROM accounts
    LEFT JOIN computers
        on computers.account_id = accounts.account_id
    LEFT JOIN users
        on users.computer_id = computers.computer_id
    LEFT JOIN files
        on files.user_id = users.user_id
    WHERE
        accounts.account_id = :account_id and
        computers.computer_name = :computer_name and
        users.username = :username
");

//bindings (test values)
$stmt->bindValue(':account_id', 1);
$stmt->bindValue(':computer_name', 'COMPUTER_2');
$stmt->bindValue(':username', 'jenny');
//execute
$stmt->execute();

//result (can only be one or none)
$results = $stmt->fetch(PDO::FETCH_ASSOC);
编辑:

这个链接有一些类似于我“认为”我需要的东西,显示了如何创建一个包含行信息的临时表

在观看了@Abhik脉轮反应后,我开始思考。我删除了min,只是将selects添加到我想要从
文件中选择的列中,它可以正常工作。为什么?因为我只返回一个结果(fetch),而
文件的联接将联接该表中与其他条件匹配的第一行。在本例中,这将是该用户具有最低文件id的行,因为文件id是自动inc主键

因此,我是否可以假设我将始终获得
文件中最低的文件id行?这就是我最初使用
MIN(files.file\u id)
的全部原因,但现在我想知道它是否有必要,因为它是一个auto inc主键。这还假设我在下面的声明中没有传递任何ORDER BY条件。知道正确的一行总是会被返回,我仍然会感到更安全

$stmt = $db->prepare("
    SELECT 
        accounts.account_id,
        accounts.account_username,
        accounts.account_password,
        computers.computer_id,
        users.user_id,
        files.file_id,
            files.filename,
            files.timestamp
    FROM accounts
    LEFT JOIN computers
        on computers.account_id = accounts.account_id
    LEFT JOIN users
        on users.computer_id = computers.computer_id
    LEFT JOIN files
        on files.user_id = users.user_id
    WHERE
        accounts.account_id = :account_id and
        computers.computer_name = :computer_name and
        users.username = :username
");

您是否在结果中只得到一个first\u file\u id列?我的查询工作如图所示,但是的,目前我只是将first\u file\u id“添加”到结果中。我需要返回的行中其余的列。考虑提供DDL和示例数据以及期望的输出。可能是开着的,我想我可以不用它就走了,但我以后再摆弄。基本上,我想包括
files
中的所有列,其中
files.file\u id
是最小的。现在我只包括那个专栏。。。我还需要行中的其余列。我看到在这种情况下,您将需要连接的透视表,让我尝试在fiddle中为u。