Mysql 在laravel中连接两个表-如何将第二个表数据作为结果属性

Mysql 在laravel中连接两个表-如何将第二个表数据作为结果属性,mysql,laravel,join,Mysql,Laravel,Join,我有两张桌子A和B 表A: no name type 1 shoe 1 2 shirt 2 3 book 3 表B: type color size 1 red big 2 yellow small 3 blue medium 当我查询A.no==1和A.type==1时,我希望得到如下数据: { no: 1, name: 'shoe', type: 1, info: { color: 'red',

我有两张桌子A和B

表A:

no  name  type
1   shoe   1
2   shirt  2
3   book   3
表B:

type color   size
1    red     big
2    yellow  small
3    blue    medium
当我查询A.no==1和A.type==1时,我希望得到如下数据:

{
  no: 1,
  name: 'shoe',
  type: 1,
  info: {
    color: 'red',
    size: 'big'
  },
}
我试过这样的方法:

select a.*, b.* from stores a, types b where a.type = 1 and a.type = b.id
它只返回普通对象,我想得到上面的嵌套数据。 我认为可以使用join和其他任何查询技巧来完成

这是我为您准备的sql fiddle链接。


提前感谢。

因此,尽管使用1992年后的查询语法编写得更好,但您所需要的查询已经足够了。问题的其余部分是重新排列结果数组的简单情况。我不知道eloquent/laravel,我在重新排列数组方面非常糟糕,但这里有一个例子,我的意思是使用普通的旧php,也许有人会编写一个更合适的数组转换

<?php

/*
DROP TABLE IF EXISTS table_a;

CREATE TABLE table_a
(no SERIAL PRIMARY KEY
,name  VARCHAR(12) UNIQUE
,type INT NOT NULL
);

INSERT INTO table_a VALUES
(1,'shoe',1),
(2,'shirt',2),
(3,'book',3);

DROP TABLE IF EXISTS table_b;

CREATE TABLE table_b
(type SERIAL PRIMARY KEY
,color VARCHAR(12) NOT NULL
,size VARCHAR(12) NOT NULL
);

INSERT INTO table_b VALUES
(1,'red','big'),
(2,'yellow','small'),
(3,'blue','medium');

SELECT a.no
     , a.name
     , b.*
  FROM table_a a
  JOIN table_b b
    ON b.type = a.type
 ORDER
    BY a.no;
+----+-------+------+--------+--------+
| no | name  | type | color  | size   |
+----+-------+------+--------+--------+
|  1 | shoe  |    1 | red    | big    |
|  2 | shirt |    2 | yellow | small  |
|  3 | book  |    3 | blue   | medium |
+----+-------+------+--------+--------+
*/

require('path/to/connection/stateme.nts');

$query = "
SELECT a.no
     , a.name
     , b.type
     , b.color
     , b.size
  FROM table_a a
  JOIN table_b b
    ON b.type = a.type
 WHERE a.no = 1
   AND a.type = 1
 ORDER
    BY a.no;
";

$result = mysqli_query($db,$query) or die(mysqli_error());

$old_array = array();

while($row = mysqli_fetch_assoc($result)){

$old_array[] = $row;

}

$new_array = array();

foreach ($old_array as $row) {
   $new_array[]['name'] = $row['name'];
   $new_array[$row['no']]['info']['color'] = $row['color'];
   $new_array[$row['no']]['info']['size'] = $row['size'];
}

$new_array = array_values($new_array); // reindex


print_r($new_array);

?>
或者,json_编码的

[{"name":"shoe"},{"info":{"color":"red","size":"big"}}]

因此,尽管使用1992年后的查询语法编写得更好,但您所需要的查询已经足够了。问题的其余部分是重新排列结果数组的简单情况。我不知道eloquent/laravel,我在重新排列数组方面非常糟糕,但这里有一个例子,我的意思是使用普通的旧php,也许有人会编写一个更合适的数组转换

<?php

/*
DROP TABLE IF EXISTS table_a;

CREATE TABLE table_a
(no SERIAL PRIMARY KEY
,name  VARCHAR(12) UNIQUE
,type INT NOT NULL
);

INSERT INTO table_a VALUES
(1,'shoe',1),
(2,'shirt',2),
(3,'book',3);

DROP TABLE IF EXISTS table_b;

CREATE TABLE table_b
(type SERIAL PRIMARY KEY
,color VARCHAR(12) NOT NULL
,size VARCHAR(12) NOT NULL
);

INSERT INTO table_b VALUES
(1,'red','big'),
(2,'yellow','small'),
(3,'blue','medium');

SELECT a.no
     , a.name
     , b.*
  FROM table_a a
  JOIN table_b b
    ON b.type = a.type
 ORDER
    BY a.no;
+----+-------+------+--------+--------+
| no | name  | type | color  | size   |
+----+-------+------+--------+--------+
|  1 | shoe  |    1 | red    | big    |
|  2 | shirt |    2 | yellow | small  |
|  3 | book  |    3 | blue   | medium |
+----+-------+------+--------+--------+
*/

require('path/to/connection/stateme.nts');

$query = "
SELECT a.no
     , a.name
     , b.type
     , b.color
     , b.size
  FROM table_a a
  JOIN table_b b
    ON b.type = a.type
 WHERE a.no = 1
   AND a.type = 1
 ORDER
    BY a.no;
";

$result = mysqli_query($db,$query) or die(mysqli_error());

$old_array = array();

while($row = mysqli_fetch_assoc($result)){

$old_array[] = $row;

}

$new_array = array();

foreach ($old_array as $row) {
   $new_array[]['name'] = $row['name'];
   $new_array[$row['no']]['info']['color'] = $row['color'];
   $new_array[$row['no']]['info']['size'] = $row['size'];
}

$new_array = array_values($new_array); // reindex


print_r($new_array);

?>
或者,json_编码的

[{"name":"shoe"},{"info":{"color":"red","size":"big"}}]
模型表A:

公共功能信息 { 返回$this->hasOneTableB::class,'type','type'; } 模型表B:

公共功能表a { 返回$this->belongsToTableA::class,'type','type'; } 查询:

TableA::with'info'->where['type'=>1,'no'=>1]->get; 模型表A:

公共功能信息 { 返回$this->hasOneTableB::class,'type','type'; } 模型表B:

公共功能表a { 返回$this->belongsToTableA::class,'type','type'; } 查询:

TableA::with'info'->where['type'=>1,'no'=>1]->get;
是的,我可以使用连接获取内联数据,但我想获取嵌套数据结构。我打开了一个sql fiddle链接。这就是雄辩关系的用途。是的,我可以使用连接获取内联数据,但我想获取嵌套数据结构。我打开了一个sql fiddle链接。这就是雄辩关系的用途。实际上,试想一下,第二张表的列太多了,很难指示所有的列。现在是时候考虑一个不同的模型了。需要一个论点。请考虑切换错误模式。实际上,想象一下第二张表有这么多的列,很难指示所有的列。现在是时候考虑一个不同的模型了,你有一个错误。需要一个论点。请考虑切换错误模式。