简体   繁体   中英

How can I query relational active record from table B with table A has two fields connect the table B's primary key?

Everyone.

I need to query a exchange rule records for my application, and the rule table like this:

and has another table like this:

And I need to get the data structure like this:

array(4) {
  [0] => array(6) {
    ["id"] => int(4)
    ["pay_goods"] => [
        "id" => 1,
        "name" => "小熊",
        "alias" => "littlebear",
        "img_url" => "xxxx",
        "desc" => "xxxxxx",
        ...
    ],
    ["goal_goods"] => [
        "id" => 2,
        "name" => "红钻",
        "alias" => "reddiamond",
        "img_url" => "xxxx",
        "desc" => "xxxxxx",
        ...
    ],
    ["exchange_ratio"] => string(8) "1:985000"
    ["is_del"] => int(0)
    ["order"] => int(4)
  },
  [1] => array(6) {
     ...
  },
  ...

How can I query out this result with one SQL sentence?

Thought for a long time and I cannt find the SQL, Please Help M

I already solved this situation in PHP Model Relationship And Thanks!

public function getExchangeGoodsList() {
    $gameGoodsConfigModel = new GameGoodsConfigModel();
    $config = $gameGoodsConfigModel::all();
    $goodsExchangeRuleList = [];
    foreach ($config as $key => $obj) {
        $itemArr = module_to_array($obj);
        $itemArr['pay_goods'] = module_to_array($obj->goods);
        $itemArr['goal_goods'] = module_to_array($obj->goalGoods);
        array_push($goodsExchangeRuleList, $itemArr);
    }
}

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