简体   繁体   English

如何从与另一个表具有一对多关系的表中选择特定列?

[英]How to select specific column from a table that has one to many relationship with another table?

I have 2 tables that have one to many relationship (users & locations) , Each user has one location , But location could have many users. 我有2个表具有一对多关系(用户和位置),每个用户有一个位置,但是位置可以有许多用户。

In users table there is a column called location_id that is related to the column called id in locations table. users表中,有一个名为location_id的列,与locations表中名为id的列相关。

locations.id -> users.location_id. location.id-> users.location_id。

So when I want to get all the users and their locations , I use this code: 因此,当我想获取所有users及其locations ,请使用以下代码:

//Select all the users.
$stmt = $pdo->prepare('SELECT * 
                        FROM users 
                            JOIN locations ON users.location_id = locations.id ');
$stmt->execute();
$values = $stmt->fetchAll(); 

//Loop through the users.
foreach($values as $val){
    $userName = $val['name'];

    //Get The location_id from users table.
    $locationId = $val['location_id']; 

    //Select the location based on this location_id.  
    $st = $pdo->prepare('SELECT * from locations WHERE id = :zid');
    $st->execute(array('zid' => $locationId));
    $v = $st->fetch();
    $location =  $v['location'];
}

Is there is a better way for getting the same result? 有没有更好的方法来获得相同的结果?

this would do the same thing as far as i can tell 据我所知,这将做同样的事情

//Select all the users.
$stmt = $pdo->prepare('SELECT * 
                        FROM users 
                            JOIN locations ON users.location_id = locations.id ');
$stmt->execute();
$values = $stmt->fetchAll(); 

//Loop through the users.
foreach($values as $val){
    $userName = $val['name'];

    //Get The location
    $location =  $val['location'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表中显示一列在Laravel中有很多关系 - Display one column from table in has many relationship in Laravel 如何根据另一个表列从一个表中选择一列? - how to select a column from one table according to another table column? 将数据从表复制到另一个表并应用一对多关系 - Copying Data from table to another table and applying one to many relationship 从一个表中选择不在另一表中但具有特定条件的记录 - Select records from one table that are not in another table but with specific conditions 如何从一对多关系mysql表中获取数据 - How to Fetch data from one to many relationship mysql table 如何通过用户操作从一个表中选择特定数据并将其一部分重新插入另一表 - How to select specific data from one table and insert a part of it back to another table by user action 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 如何从另一个表中选择表中的特定条件 - how to select a specific condition in table from another table Laravel select 如果 id 存在于另一个表中并且列具有该表上的特定值,则所有行 - Laravel select all rows if id exists in another table and a column has specific values on that table 如何从具有一对多关系的表中选择一行结果 - How to select from table with one to many relation to be one row result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM