根据外键获取另一列的值

时间:2015-11-03 04:58:58

标签: php mysql

我在网络应用中为管理员显示视图中表格的所有数据。

SQL看起来像这样:

$organizations = $db->query("
    SELECT id, organization_name, owner_id
    FROM organizations
    ORDER BY created_on DESC
  ")->fetchALL(PDO::FETCH_ASSOC);

我正在使用的视图部分如下:

<?php foreach($organizations as $organization): ?>
   <tr>
     <td><?php echo e($organization['organization_name']); ?></td>
     <td><?php echo e($organization['owner_id']); ?></td>
   </tr>
<?php endforeach; ?>

这完全符合预期,但实际上并不是我想要显示的owner_idusers表的int和主键)

这将生成一个包含SQL语句中所有值的表,特别是它将owner_id呈现给视图,该视图是与我的users表相关的外键。

我想要做的是实际显示属于name的所有者的owner_id,而不是仅显示id(即... 32)。如何根据引用的外键nameusers表中显示用户的关联user_id

2 个答案:

答案 0 :(得分:2)

您可以使用JOIN

[root@sandbox ~]# pip install fiona

   fiona/ogrext.c: In function ‘pyx_f_5fiona_6ogrext_14FeatureBuilder_build’:

    fiona/ogrext.c:3255: warning: assignment discards qualifiers from pointer target type
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_7Session_4start’:
    fiona/ogrext.c:8043: warning: implicit declaration of function ‘OGR_L_GetName’
    fiona/ogrext.c:8043: warning: assignment makes pointer from integer without a cast
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_7Session_14get_driver’:
    fiona/ogrext.c:8912: warning: assignment discards qualifiers from pointer target type
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_7Session_16get_schema’:
    fiona/ogrext.c:9162: warning: assignment discards qualifiers from pointer target type
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_7Session_18get_crs’:
    fiona/ogrext.c:10069: warning: assignment discards qualifiers from pointer target type
    fiona/ogrext.c:10078: warning: assignment discards qualifiers from pointer target type
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_7Session_22get_extent’:
    fiona/ogrext.c:11112: warning: passing argument 2 of ‘OGR_L_GetExtent’ from incompatible pointer type
    /usr/include/gdal/ogr_api.h:324: note: expected ‘struct OGREnvelope *’ but argument is of type ‘struct pyx_t_5fiona_6ograpi_OGREnvelope *’
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_14WritingSession_start’:
    fiona/ogrext.c:13233: warning: statement with no effect
    fiona/ogrext.c:14390: warning: assignment makes pointer from integer without a cast
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_13_listlayers’:
    fiona/ogrext.c:19743: warning: assignment makes pointer from integer without a cast
    fiona/ogrext.c: In function ‘pyx_pf_5fiona_6ogrext_15buffer_to_virtual_file’:
    fiona/ogrext.c:19866: error: ‘VSILFILE’ undeclared (first use in this function)
    fiona/ogrext.c:19866: error: (Each undeclared identifier is reported only once
    fiona/ogrext.c:19866: error: for each function it appears in.)
    fiona/ogrext.c:19866: error: ‘pyx_v_vsi_handle’ undeclared (first use in this function)
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-oCUpd1/fiona/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-8szSWo-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-oCUpd1/fiona

然后在视图中,它可以用作

$organizations = $db->query("
    SELECT organizations.id, organizations.organization_name, 
    users.user_name
    FROM organizations
    JOIN users ON organizations.owner_id = users.user_id
    ORDER BY organizations.created_on DESC
  ")->fetchALL(PDO::FETCH_ASSOC);

答案 1 :(得分:1)

您需要使用JOIN链接两个表。以下示例链接owner_id上的两个表格,并在结果中包含user_name。如果两个表中都存在任何列名,则需要在SELECT中使用别名。

-- use alias and include user_name
SELECT o.id, o.organization_name, u.user_id, u.user_name 
-- alias the table as "o"
FROM organizations o 
-- alias the table as "u"
JOIN users u 
    -- link the tables here on owner_id
    ON o.owner_id = u.user_id 
ORDER BY o.created_on DESC

然后,您可以在PHP中输出user_name列的值,如下所示:

<td><?php echo e($organization['user_name']); ?></td>