php - 将父表连接到其子表并从中获取数据

标签 php mysql laravel

我有两个数据库。 parentSubchildsub

父子

|ID|component_name|weight|actualWeight
 1   a              0        0
 2   b              0        0
 3   c              0        0
 4   d              0        0
 5   e              0        0
 6   f              0        0

子子

|parent_iD|category_name|weight|actualWeight
 1          aa              0        0
 2          aaa             0        0
 3          aaaa            0        0
 4          bb              0        0
 5          bbb             0        0
 6          cc              0        0
 7          ccc             0        0
 8          dd              0        0
 9          ee              0        0
 10         ff              0        0

正如您在上面看到的那样,在这种情况下,childSub 有名为parent_iD 的列。我想获取名为 component_name 的列,之后子数据必须位于 ParentSub 中。像这样。我怎样才能获得该特定列下的childsub。所有 a 必须位于父 a 下。使用他们的 ID。

 a
    aa
    aaa
    aaaa
  b
    bb  
    bbb
  c
    cc
    ccc
  d 
    dd
  e
    ee
  f
    ff

我一直在使用 Laravel 作为框架。我所做的唯一一件事就是仅获取 component_name 列和 category_name

getColumnController.php

 return View::make('view.index')->with('parentSubs',parentSub::get())->with('childSubs',childSubs::get());

查看

<table class="table datatable_simple">
                <tbody>
                    @foreach($parentSubs as $parentSub)
                        <tr> 
                            <td><em>{{{$parentSub->component_name}}}</em></td>
                        </tr>
                    @endforeach
                </tbody>
                <tbody>
                   @foreach($childSubs as $childSub)
                    <tr>
                        <td>{{{$childsub->survey_question}}}</td>   

                    </tr>
                    @endforeach
                </tbody>
            </table>

最佳答案

尝试以下:

select (select b.component_name from parentSub b where b.ID = a.parent_iD) as component_name ,a.category_name,a.weight,a.actualWeight from childSub a

关于php - 将父表连接到其子表并从中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881338/

相关文章:

php - TYPO3:如何在 Extbase 扩展中使用外部 PHP 库(不安装 composer)

php - 使用ajax动态显示html表行中的sql数据

php - 将文件从 localhost 传输到 Bluehost

php - 如何使用 Ajax、PHP 和 JQuery 检查多个输入复选框?

php 登录表单总是带我到 editor.php 页面

mysql - 为什么我无法通过Vue从mysql获取新页面的数据?

javascript - 如何使用后端发送的json数据

php - 多个关键词触发数据库搜索结果

mysql - laravel mysql 连接数据库失败

laravel - 如何在不使用 curl 和 guzzle 的情况下从 Controller Laravel 调用 API,因为它不起作用