简体   繁体   English

关于获取与一行相关的几行的MySQL查询问题

[英]MySql Query issue regarding get several rows related to one row

I have table like below in my MySql database. 我的MySql数据库中有如下表。

table_books table_books

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

I would like to get result like below 我想得到如下结果

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

I need the SQL Query. 我需要SQL查询。 I need to pick up one row(book_author) with other associate rows. 我需要拿起另一行(book_author)与其他关联行。

Thanks 谢谢

<?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'];
                }
            }
        }
    }
?>

Try this. 尝试这个。 Hopefully, it'll work. 希望它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM