dousi2013 2015-06-27 08:52
浏览 20

如何获得成果[关闭]

I am facing a problem regarding display results from a mysql table. I have a table like this in my MySql Database.

Table name - books

+----+--------------------------+--------+-------+-------+----------------+
| id |           name           | author | price | pages |     topic      |
+----+--------------------------+--------+-------+-------+----------------+
|  1 | HTML for beginners       | Sanju  |   200 |   400 | HTML           |
|  2 | Master in JavaScript     | Sanju  |   300 |   500 | JavaScript     |
|  3 | Object Oriented PHP      | Henry  |   200 |   500 | php            |
|  4 | Advance AngularJS        | Henry  |   100 |   400 | AngularJs      |
|  5 | Basic Computer Operation | Fred   |   200 |   300 | Basic Computer |
|  6 | Rock the world with CSS  | Henry  |   400 |   500 | Css            |
|  7 | Be perfect in browsers   | Mark   |   250 |   300 | browser        |
+----+--------------------------+--------+-------+-------+----------------+

I would like to display as below.

+---------------------+---------------+-------+-----------+
|     Author Name     |     Henry     |       |           | 
+---------------------+---------------+-------+-----------+
+-------------------------+---------------+-------+-----------+
|        Book name        |         Price | pages |   topic   |
+-------------------------+---------------+-------+-----------+
| Object Oriented PHP     |           200 |   500 | php       |
| Advance AngularJS       |           100 |   400 | AngularJs |
| Rock the world with CSS |           400 |   500 | Css       |
+-------------------------+---------------+-------+-----------+
+---------------------+---------------+-------+-----------+
|     Author Name     |     Sanju     |       |           | 
+---------------------+---------------+-------+-----------+
+----------------------+-------+-------+------------+
|      Book name       | Price | pages |   topic    |
+----------------------+-------+-------+------------+
| HTML for beginners   |   200 |   400 | HTML       |
| Master in JavaScript |   300 |   500 | JavaScript |
+----------------------+-------+-------+------------+
+---------------------+---------------+-------+-----------+
|     Author Name     |     Fred      |       |           | 
+---------------------+---------------+-------+-----------+
+--------------------------+-------+-------+----------------+
|        Book name         | Price | pages |     topic      |
+--------------------------+-------+-------+----------------+
| Basic Computer Operation |   200 |   300 | Basic Computer |
+--------------------------+-------+-------+----------------+
+---------------------+---------------+-------+-----------+
|     Author Name     |     Mark      |       |           | 
+---------------------+---------------+-------+-----------+
+------------------------+-------+-------+---------+
|       Book name        | Price | pages |  topic  |
+------------------------+-------+-------+---------+
| Be perfect in browsers |   250 |   300 | browser |
+------------------------+-------+-------+---------+

I need both php and mysql code. Thanks

  • 写回答

1条回答 默认 最新

  • douan9541 2015-06-27 09:05
    关注

    It seems you simply need to order your data by author, however there is no ordering that puts them in this order:

    • Henry
    • Sanju
    • Fred
    • Mark

    Because this is neither alphabetical ascending or descending?

    The following queries will enable you to order by author ASC/DESC:

    SELECT * FROM books ORDER BY author ASC;
    SELECT * FROM books ORDER BY author DESC;
    

    However if you need to output in this strange order, you'll likely need to retrieve each author's books individually and output them one by one:

    SELECT * FROM books WHERE author = 'Henry';
    SELECT * FROM books WHERE author = 'Sanju';
    SELECT * FROM books WHERE author = 'Fred';
    SELECT * FROM books WHERE author = 'Mark';
    

    I wont however be posting how to retrieve the data from the database, for that you can read online tutorials for PHP/MySQL code help. See the below link for a friendly PDO reference:

    http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

    Hope this helps - sorry I can't be of any more help.

    Goodluck!

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题