在Php中显示两个表数据
我有以下几张表 作者在Php中显示两个表数据,php,mysql,sql,Php,Mysql,Sql,我有以下几张表 作者 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 * fro
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
我试过group concat,但它给出了带有coma separate的图书名称。所以我想在数组中显示图书的详细信息
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
)
)
)
我也检查了这个问题
有谁能帮我显示上面的数据吗?
谢谢试试这个:
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..
$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'];
}
$query = "select id, author_name, whatever from author";
$author_records = $database->Execute($query);
foreach ($author_records as $fields) {
echo "Author: {$fields['author_name']} whatever <br/>";
$subquery = "select whatever from books where id = whatever";
$book_records = $database->Execute($subquery);
foreach ($book_records as $otherfields) {
echo "Book whatever whatever";
}
}
要使数组成为预期结果,需要重新构造数组结果。我将评估您的结果数组是否在$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\u result
的键重置为递增数,您只需使用函数获取值
您可以使用第一个查询完成此操作…但是您必须在记录循环中检查author_id,并且仅在值更改时显示author详细信息(通过将其与存储在变量中的值进行比较)…否则仅显示book详细信息 因此,您的代码可能(非常粗略)如下所示:
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..
$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'];
}
$query = "select id, author_name, whatever from author";
$author_records = $database->Execute($query);
foreach ($author_records as $fields) {
echo "Author: {$fields['author_name']} whatever <br/>";
$subquery = "select whatever from books where id = whatever";
$book_records = $database->Execute($subquery);
foreach ($book_records as $otherfields) {
echo "Book whatever whatever";
}
}
一种更直接(虽然稍长)的方法是使用第二个查询:子查询。它将通过第一个(外部)查询的结果在循环内部进行。因此,您的外部查询将获取作者,在显示作者详细信息后,您将对作者的书籍进行单独的查询……并且在外部循环中有一个循环来显示每本书的详细信息
因此,您的代码(非常粗略)如下所示:
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..
$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'];
}
$query = "select id, author_name, whatever from author";
$author_records = $database->Execute($query);
foreach ($author_records as $fields) {
echo "Author: {$fields['author_name']} whatever <br/>";
$subquery = "select whatever from books where id = whatever";
$book_records = $database->Execute($subquery);
foreach ($book_records as $otherfields) {
echo "Book whatever whatever";
}
}
$query=“选择id、作者名称、作者的任何内容”;
$author\u records=$database->Execute($query);
foreach($author\u记录为$fields){
echo“Author:{$fields['Author_name']}无论什么
”;
$subquery=“从id=which的书籍中选择which”;
$book_records=$database->Execute($subquery);
foreach($book_记录为$otherfields){
呼应“无论什么书”;
}
}
您可以在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。要获得类似的数组键,您可以使用
数组值($item)
可能重复的@AshwiniAgarwal。它不重复。请阅读我的问题查看问题,它将为您提供解决问题的方法。@AshwiniAgarwal.ok谢谢。但我没有尝试过如何做,因为这将返回单独的数组,如[2]=>array([id]=>2[name]=>david[books]=>array([0]=>array([id]=>2[book\u name]=>book2)))书籍中的id是您的作者id或书籍id?如果是书籍id,您需要先选择书籍id。然后从$value['book\u id'解析到数组。谢谢。你的回答解决了我的问题。非常感谢。像上面这样存储数据是一种好的做法还是group concat好?哪一种更好?如果你用group_concat设置结果,那么当书名有逗号(“,”)时就会出错我不确定书名是否允许逗号。顺便说一句,您可以将分隔符组定义为另一个分隔符。但默认情况下,结果限制为1024。您可以更改默认长度。我不能说当前的算法是否更好。这两种算法都在工作。:)