简体   繁体   English

内部联接导致不同的数组

[英]Inner join results in different arrays

I am using an inner join statement to retrieve data from both the tables Venues and Users. 我正在使用内部连接语句从表Venues和Users中检索数据。 However, I would like to have an array looking like this 但是,我想有一个像这样的数组

$resultsFromVenueTableArray = array(

    $relevantVenueKey => $relevantVenueValue,
    $resultsFromUserTable => $arrayOfValuesFromUserTable

);

rather than just one array full of all of the retrieved data looking something like this: 而不是一个仅包含所有检索到的数据的数组,看起来像这样:

$resultsFromQuery = array(

    $venueKey,
    $userKey,
    $venueKey...

)

How could I achieve this? 我怎样才能做到这一点?

Here's my code; 这是我的代码;

$query = $this->db->query("SELECT * FROM Venues INNER JOIN Users ON Users.id = $userID", array($userID));

$venues = array();

foreach ($query->result() as $row) {

    $row->username = $username;
    $venues[] = $row;

}

It isn't clear from the question exactly what you would like to do, but you can pull out specific columns from both tables like so: 从问题中不清楚您到底想做什么,但是您可以像这样从两个表中提取特定的列:

SELECT Venues.*, Users.username FROM Venues INNER JOIN Users ON...

which would allow you to pull out only the 'username' column from 'Users'. 这将允许您仅从“用户”中拉出“用户名”列。

A join is always going to leave you with an array of rows, if you need something else you'll probably have to manipulate the data once you've got it in PHP unless I've misunderstood your question. 联接总是会给您留下一系列的行,如果您需要其他东西,则一旦在PHP中获得数据,您可能就不得不操纵数据,除非我误解了您的问题。

Something like this would get you all venues by a user, with the username as the array key: 这样的事情将使用户获得所有场所,用户名作为数组键:

$venuesByUser = array();
foreach ($query->result() as $row) {
    $venuesByUser[$row->username][] = $row;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM