我有以下表格
作者
id | author_name | author_detail | author_bio
书
id | author_id | book_name | book_detail
我希望以下列方式显示数据
Author Name ::
Author Detail ::
books :: 1.book1
2.book2
3.book3
4.book4
我尝试了以下查询,但根据我的要求没有工作
select * from authors left join books on author.id=books.author_id
我已经尝试了群组连续但是它给了coma seperate的书名。所以我想要在数组中预订详细信息
select author.author_name,author.author_detail,author.author_bio,group_concat(books.book_name) eft join books on author.id=books.author_id
我正在考虑输出
Array
(
[0] => Array
(
[id] => 1
[name] => Norm
[books] => Array
(
[0] =>Array
(
[id] => 4
[book_name] => great wall
[created_at] => 2015-09-11 04:45:07
[updated_at] => 2015-09-11 04:45:07
)
[1] =>Array
(
[id] => 6
[book_name] =>new book
[created_at] => 2015-09-11 04:45:07
[updated_at] => 2015-09-11 04:45:07
)
)
)
[1] => Array
(
[id] => 2
[name] => Norm
[books] => Array
(
[0] =>Array
(
[id] => 2
[book_name] => amazing star
[created_at] => 2015-09-11 04:45:07
[updated_at] => 2015-09-11 04:45:07
)
[1] =>Array
(
[id] => 3
[book_name] =>way of journy
[created_at] => 2015-09-11 04:45:07
[updated_at] => 2015-09-11 04:45:07
)
)
)
我也检查了这个问题
displaying php mysql query result with one to many relationship
任何人都可以帮我如何显示上面的数据吗? 谢谢
答案 0 :(得分:0)
试试这个:
SELECT
A.id
A.author_name,
A.author_detail,
A.author_bio,
B.book_name,
B.created_at,
B.updated_at
FROM books AS B
LEFT JOIN author AS A
ON (A.id=B.author_id)
你会得到这样的结果:
id | author_name | author_detail | author_bio | book_name
1 | ari | some detail | some bio | book_ari_1
1 | ari | some detail | some bio | book_ari_2
1 | ari | some detail | some bio | book_ari_3
2 | tester | some detail | some bio | book_tester_1
etc..
要使数组成为您期望的结果,您需要重构数组结果。我将假设您的结果数组将在$result
变量
$new_result = array();
foreach ($result as $key => $value) {
if (empty($new_result[$value['id']]))
{
$new_result[$value['id']] = array(
'id' => $value['id'],
'name' => $value['name'],
'books' => array(
array(
'id' => $value['id'],
'book_name' => $value['book_name'],
'created_at' => $value['created_at'],
'updated_at' => $value['updated_at']
),
)
)
}
else
{
$new_result[$value['id']]['id'] = $value['id'];
$new_result[$value['id']]['name'] = $value['name'];
$new_result[$value['id']]['books'][] = array(
'id' => $value['id'],
'book_name' => $value['book_name'],
'created_at' => $value['created_at'],
'updated_at' => $value['updated_at']
);
}
}
the result will look like your expected. but the key number will be formated as id.
重置$new_result
的密钥作为增量编号,只需使用值array_values()
函数
$new_result = array_values($new_result);
答案 1 :(得分:0)
您可以使用第一个查询来执行此操作...但您必须检查记录循环内的author_id,并仅在值更改时显示作者详细信息(通过将其与存储在变量中的值进行比较) )...否则只显示书的详细信息。
所以你的代码可能(非常粗略地)看起来像这样:
public void addData()
{
ListSerialPorts listSerPortObj = new ListSerialPorts();
listData = listSerPortObj.LoadComPorts2();
for (int index = 0; index < listData.size(); index++)
{
str = (String) listData.get(index);
System.out.println(str);
jComboBox2.addItem(str);
}
更简单(虽然稍长)的方法是进行第二次查询:子查询。它将通过第一个(外部)查询的结果在循环内部发生。因此,您的外部查询将获得作者,并且在您显示作者详细信息之后,您将对作者的书籍进行单独查询...并且您在外部循环中有一个循环来显示每本书的详细信息。
所以你的代码(非常粗略)看起来像这样:
$query = "select whatever whatever...";
$records = $database->Execute($query);
foreach ($records as $fields) {
if ($fields['id'] != $previous_id) echo "Author ...";
echo "Book whatever whatever ...";
$previous_id = $fields['id'];
}
答案 2 :(得分:-1)
你可以在php中执行此操作,无需进入查询本身,但在单独的查询中获取两个数据,即书籍和作者数据
请记住,我假设$result
为作者数据,$result2
为图书数据
$item=array();
while($row=mysql_fetch_array($result))
{
$id=$row['id'];
$item[$id]['name']=$row['name'];
$item[$id]['id']=$row['id'];
$item[$id]['books']=array();
$temp=array();
while($row1=mysql_fetch_array($result2))
{
if($id==$row1['author_id'])
{
$temp['id']=$row1['id'];
$temp['book_name']=$row1['book_name'];
$temp['created_at']=$row1['created_at'];
$temp['updated_at']=$row1['updated_at'];
array_push($item['id']['books'],$temp);
}
}
}
现在这里的id被格式化为作者的id。要获得类似的数组键,您可以使用array_values($item)