如何获得成果[关闭]

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

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!