Php 从表创建嵌套数组的最佳方法:多查询/循环与单查询/循环样式
假设我有两张桌子, 我可以将其“合并”并在单个嵌套数组中表示 我在想什么是最好的方法,考虑到:Php 从表创建嵌套数组的最佳方法:多查询/循环与单查询/循环样式,php,mysql,sql,relational-database,server-side,Php,Mysql,Sql,Relational Database,Server Side,假设我有两张桌子, 我可以将其“合并”并在单个嵌套数组中表示 我在想什么是最好的方法,考虑到: 效率 最佳做法 数据库/服务器端使用权衡 你在现实生活中应该做什么 对于3个、4个或更多可以通过这种方式“合并”的表,情况也是如此 问题是关于任何服务器端/关系数据库。 我想的两个简单的方法 (如果你有其他的,请建议! 请注意,我要的是一个简单的服务器端和关系数据库, 所以请不要浪费时间解释为什么我不应该 使用这种数据库,使用MVC设计等……): 2个循环,5个简单的“选择”查询 1个循环,1个“
- 效率
- 最佳做法
- 数据库/服务器端使用权衡
- 你在现实生活中应该做什么
- 对于3个、4个或更多可以通过这种方式“合并”的表,情况也是如此
此链接是另一个问题的示例,我尝试提出第二种方法:一般来说,最好在尽可能少的数据库访问中获取所需的数据,然后将数据映射到适当的对象中。(备选方案2) 但是,为了回答您的问题,我会问您自己数据的用例是什么。如果你确信你需要你的人和电话号码数据,那么我会说第二种方法是你最好的选择 但是,当连接的数据是可选的时,选项一也可以有它的用例。一个例子是,在UI上,你有一个包含所有人的表,如果用户想查看某个特定人的电话号码,他们必须单击该人。然后可以“延迟加载”所有电话号码。 首先,感谢您花那么多精力解释这个问题,并提供了格式。很高兴看到有人清楚他们在做什么,他们在问什么 但必须注意的是,这本身就形成了一个局限性:你认为这是正确的解决方案,只要稍加纠正或指导,就会奏效。这是不正确的。因此,我必须要求你们放弃这个想法,退后一大步,看看(a)整个问题,(b)我的答案没有这个想法 这个答案的背景是:
- 你提出的所有明确的考虑,这些都非常重要,我将不再重复
- 其中最重要的两个问题是,什么是最佳实践,以及我在现实生活中会做什么
- 一些数据库系统没有服务器体系结构,因此这种软件中的服务器概念是错误的和误导性的,这是单独的,但值得注意的一点
- 这打开了一罐蠕虫。如果您没有正确地设计和实现此功能,蠕虫将溢出应用程序
- 这样的实现严重违反了客户机/服务器体系结构,该体系结构允许双方编写简单的代码,并适当部署软件和数据组件,从而实现时间短,效率高
- 此外,这种实现需要大量的实现工作,而且非常复杂,由许多部分组成。这些部件中的每一个都必须进行适当的设计
- 网络,以及在这个主题领域写的许多书,提供了一个混乱的方法组合,在假定的简单性的基础上进行营销;缓解任何人都可以做任何事;免费软件可以做任何事情;这些建议都没有科学依据
CREATE TABLE persons
(
id int NOT NULL AUTO_INCREMENT,
fullName varchar(255),
PRIMARY KEY (id)
);
INSERT INTO persons (fullName) VALUES ('Alice'), ('Bob'), ('Carl'), ('Dan');
CREATE TABLE phoneNumbers
(
id int NOT NULL AUTO_INCREMENT,
personId int,
phoneNumber varchar(255),
PRIMARY KEY (id)
);
INSERT INTO phoneNumbers (personId, phoneNumber) VALUES ( 1, '123-456'), ( 1, '234-567'), (1, '345-678'), (2, '456-789'), (2, '567-890'), (3, '678-901'), (4, '789-012');
[
{
"id": 1,
"fullName": "Alice",
"phoneNumbers": [
"123-456",
"234-567",
"345-678"
]
},
{
"id": 2,
"fullName": "Bob",
"phoneNumbers": [
"456-789",
"567-890"
]
},
{
"id": 3,
"fullName": "Carl",
"phoneNumbers": [
"678-901"
]
},
{
"id": 4,
"fullName": "Dan",
"phoneNumbers": [
"789-012"
]
}
]
query: "SELECT id, fullName FROM persons"
personList = new List<Person>()
foreach row x in query result:
current = new Person(x.fullName)
"SELECT phoneNumber FROM phoneNumbers WHERE personId = x.id"
foreach row y in query result:
current.phoneNumbers.Push(y.phoneNumber)
personList.Push(current)
print personList
query: "SELECT persons.id, fullName, phoneNumber FROM persons
LEFT JOIN phoneNumbers ON persons.id = phoneNumbers.personId"
personList = new List<Person>()
current = null
previouseId = null
foreach row x in query result:
if ( x.id != previouseId )
if ( current != null )
personList.Push(current)
current = null
current = new Person(x.fullName)
current.phoneNumbers.Push(x.phoneNumber)
print personList
/* get all persons */
$result = mysql_query("SELECT id, fullName FROM persons");
$personsArray = array(); //Create an array
//loop all persons
while ($row = mysql_fetch_assoc($result))
{
//add new person
$current = array();
$current['id'] = $row['id'];
$current['fullName'] = $row['fullName'];
/* add all person phone-numbers */
$id = $current['id'];
$sub_result = mysql_query("SELECT phoneNumber FROM phoneNumbers WHERE personId = {$id}");
$phoneNumbers = array();
while ($sub_row = mysql_fetch_assoc($sub_result))
{
$phoneNumbers[] = $sub_row['phoneNumber']);
}
//add phoneNumbers array to person
$current['phoneNumbers'] = $phoneNumbers;
//add person to final result array
$personsArray[] = $current;
}
echo json_encode($personsArray);
/* get all persons and their phone-numbers in a single query */
$sql = "SELECT persons.id, fullName, phoneNumber FROM persons
LEFT JOIN phoneNumbers ON persons.id = phoneNumbers.personId";
$result = mysql_query($sql);
$personsArray = array();
/* init temp vars to save current person's data */
$current = null;
$previouseId = null;
$phoneNumbers = array();
while ($row = mysql_fetch_assoc($result))
{
/*
if the current id is different from the previous id:
you've got to a new person.
save the previous person (if such exists),
and create a new one
*/
if ($row['id'] != $previouseId )
{
// in the first iteration,
// current (previous person) is null,
// don't add it
if ( !is_null($current) )
{
$current['phoneNumbers'] = $phoneNumbers;
$personsArray[] = $current;
$current = null;
$previouseId = null;
$phoneNumbers = array();
}
// create a new person
$current = array();
$current['id'] = $row['id'];
$current['fullName'] = $row['fullName'];
// set current as previous id
$previouseId = $current['id'];
}
// you always add the phone-number
// to the current phone-number list
$phoneNumbers[] = $row['phoneNumber'];
}
}
// don't forget to add the last person (saved in "current")
if (!is_null($current))
$personsArray[] = $current);
echo json_encode($personsArray);
SELECT ... FROM Person JOIN PhoneNumber
SELECT id, fullName From Person;
SELECT personId, phoneNumber FROM phoneNumbers
WHERE personId IN (SELECT id From Person);
personList = ConvertToEntity<List<Person>>(dataset.Table[0]);
pnoList = ConvertToEntity<List<PhoneNumber>>(dataset.Table[1]);
foreach (person in personList) {
foreach (pno in pnoList) {
if(pno.PersonId = person.Id)
person.PhoneNumer.Add(pno)
}
}