duanlvxi8652 2015-05-16 09:35
浏览 57

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

I have table like below in my MySql database.

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. I need to pick up one row(book_author) with other associate rows.

Thanks

  • 写回答

1条回答 默认 最新

  • dongyue7796 2015-05-16 10:09
    关注
    <?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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)