【问题标题】:Use subqueries to assign column values for each record in laravel使用子查询为 laravel 中的每条记录分配列值
【发布时间】:2019-06-10 00:35:06
【问题描述】:

据我所知,“子查询可以为每条记录分配列值”。

示例:让我们考虑这个数据库,

Example database

user (id, name, age)
user_detail (user_id[foreign], user_email, address)  

现在我们可以像这样通过子查询选择所有电子邮件和姓名:

    SELECT id, (SELECT user_email FROM user_detail WHERE user_detail.user_id = user.id LIMIT 1) as email,
    name, age 
       FROM user
          WHERE 1

这将输出如下表:

 _ _ _ _ _ _ _ _ _ _ _ _ _
| id | email | name | age |
+-------------------------+
---------All rows----------

现在我如何用它来查询 laravel eloquent?

更具体... 我有几张桌子,

1. session (id, name)
2. account (id, name, group)
3. action (id, action_name, detail)
4. another_table (id, detail)
5. transaction (id, from_account_id, to_account_id, session_id,
   action_table_id, another_table_id, other_attributes )

现在我想要一个查询来将每一行作为一个对象,并挖一个数组 查询应该返回

result (transaction_id, session_name, from_account_name, to_account_name,
        action_name, another_table_detail, other_attributes)

最后将它们发送到 json,这样我就可以通过一个 for 循环来读取它们。

【问题讨论】:

  • 为什么必须使用子查询?你熟悉Eloquent relationships吗?
  • 感谢您的回复#Jonas。我想用 laravel 学习子查询并将它们应用于 4 到 5 个表连接。如果我使用关系比雄辩(据我所知)变得如此昂贵。这就是拖的原因。你能帮我联系吗?
  • 好吧,更具体...让我们有一些表,1. session (id, name) 2. account (id, name, group) 3. action (id, action_name, detail) 4. another_table (id, detail) 5. transaction (id, from_account_id, to_account_id, session_id, action_table_id, another_table_id, other_attributes ) 现在我想要一个查询来将每一行作为一个对象,并且数组查询应该返回结果 (transaction_id, session_name , from_account_name, to_account_name, action_name, another_table_detail, other_attributes) 最后将它们发送到 json,这样我就可以通过 for 循环读取它们。
  • 您可以使用selectSub() 创建子查询。
  • 非常感谢。我正在寻找这个。它适用于 selectSub()。

标签: laravel eloquent


【解决方案1】:

感谢Jonas-staudenmeir.他帮助我找到了解决方案。

在这种情况下,我们可以使用 laravel 查询构建器类中的 selectSub() 方法。 selectSub()

第一个:

    $user_email = DB::table('user_detail')
                ->select('user_detail.user_email')
                ->whereRaw('user_detail.user_id = user.id')
                ->take(1);

    $data = DB::table('user')
            ->select('name', 'age')
            ->selectSub($user_email, 'email') // selectSub(sub_query, as) 
            ->get();

第二个:

    //session query
    $session = DB::table('session')
                ->select('session.name')
                ->whereRaw('transaction.session_id = session.id')
                ->take(1);
    //from_account query
    $from_account = DB::table('account')
                ->select('account.name')
                ->whereRaw('transaction.from_account_id = account.id')
                ->take(1);
    //to_account query
    $to_account = DB::table('account')
                ->select('account.name')
                ->whereRaw('transaction.to_account_id = account.id')
                ->take(1);
    //action query
    $action = DB::table('action')
                ->select('action.action_name')
                ->whereRaw('transaction.action_table_id = action.id')
                ->take(1);
    //another_table query
    $another_table = DB::table('another_table')
                ->select('another_table.detail')
                ->whereRaw('transaction.another_table_id = another_table.id')
                ->take(1);

    $data = DB::table('transaction')
            ->select('transaction.id as transaction_id', 'other_attributes')
            ->selectSub($session, 'session_name') //session as session_name
            ->selectSub($from_account, 'from_account_name') //from_account as from_account_name
            ->selectSub($to_account, 'session_name') //to_account as to_account_name
            ->selectSub($action, 'action_name') //action as action_name
            ->selectSub($another_table, 'another_table_detail') //another_table as another_table_detail
            ->get();

虽然我们可以使用联接或左联接。哪个更快,这完全取决于数据、索引、相关性、数据量、查询等。子查询可能比 LEFT [OUTER] JOINS 慢,但在我看来,它们的优势在于可读性略高。 join-vs-sub-query

【讨论】:

    【解决方案2】:

    试试这个

    $users = User::join('users', 'users.id', '=', 'user_detail')->get();
    

    应该用你需要的所有记录连接两个表。

    【讨论】:

    • 感谢您的回复#rkg。我有这个想法,这是一个很好的解决方案。但我想从子查询中使用它。我坚持下去。你能帮我联系吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多