gpt4 book ai didi

php - 关于获取与一行相关的多行的MySql查询问题

转载 作者:行者123 更新时间:2023-11-29 22:25:53 26 4
gpt4 key购买 nike

我的 MySql 数据库中有如下表。

表书

book_author         book_name                       book_page   book_price  book_published_date book_topic

James Anderson Introduction to PHP 200 50 1/12/2013 Science
James Anderson Expert in jQuery 150 40 8/7/2014 Programming
James Anderson HTML 200 60 5/9/2012 Web
Richard Benjamin Successful stories of Business 300 70 4/6/2014 Business
Richard Benjamin Entrepreneurship 500 80 8/9/2013 Business
Richard Benjamin Business Studies 100 40 2/5/2012 Business

我想得到如下结果

book_author James Anderson              

book_name book_page book_price book_published_date book_topic

Introduction to PHP 200 50 1/12/2013 Science
Expert in jQuery 150 40 8/7/2014 Programming
HTML 200 60 5/9/2012 Web




book_author Richard Benjamin


book_name book_page book_price book_published_date book_topic

Successful stories of Business 300 70 4/6/2014 Business
Entrepreneurship 500 80 8/9/2013 Business
Business Studies 100 40 2/5/2012 Business

我需要 SQL 查询。我需要选取一行(book_author)和其他关联行。

谢谢

最佳答案

<?php
$sqlAuthor = "SELECT DISTINCT book_author FROM book_name";
$exeAuthor = mysql_query($sqlAuthor);
if($exeAuthor)
{
while($dataAuthor = mysql_fetch_assoc($exeAuthor))
{
$sqlBookRec = "SELECT * FROM book_name where book_author='".$dataAuthor['book_author']."'";
$exeBookRec = mysql_query($sqlBookRec);
if($exeBookRec)
{
echo "Book Author: ".$dataAuthor['book_author'];
while($dataBookRec = mysql_fetch_assoc($exeBookRec))
{
echo "Book Name".$dataBookRec['book_name'];
}
}
}
}
?>

试试这个。希望它能发挥作用。

关于php - 关于获取与一行相关的多行的MySql查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com