Home > Blockchain >  Mysql join promblem
Mysql join promblem

Time:10-03

My query:

SELECT * 
FROM outlets 
INNER JOIN users ON outlets.owner=users.id;

The result is an array in which the indexes from one table match those of another table. Is it possible to somehow make such a request that owner = array, and there is already a structure Result:

Array ( [id] => 1 [owner] => 1 [name] => Рома [email] => admin [username] => admin [password] => admin [role] => 1 [photo] => [permissions] => {"menu": {"menu.shop": 1, "menu.users": 1, "menu.stocks": 1, "menu.clients": 1, "menu.finances": 1, "menu.partners": 1, "menu.products": 1, "menu.purchase": 1, "menu.dashboard": 1}} [status] => 1 )

CodePudding user response:

To return a result as an associative array use

CodePudding user response:

If I understood correctly. You want to have an owner value containing data of the user (but not having data on the same level).

Option 1: By SQL you can do 2 separate queries and join results manually by PHP. But then you may have performance penalties.

Option 2: As a recommendation, if you need such a payload, just reformat your array as desired manually like this:

$originalArray = ['id' => 1, 'owner' => 1, 'name' => 'test', 'username' => 'test'];

$reformatedArray = [];
foreach($originalArray as $values){
    $reformatedArray['id'] = $values['id'];
    $reformatedArray['owner']['name'] = $values['name'];
    $reformatedArray['owner']['name'] = $values['username'];
    // ...
}

print_r($reformatedArray);

// ['id' => 1, 'owner' => ['name' => 'test', 'username' => 'test']]

Hope it helps.

  • Related