简体   繁体   中英

Inner join results in different arrays

I am using an inner join statement to retrieve data from both the tables Venues and 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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