如何获得成功结果

时间:2015-06-27 08:52:34

标签: php mysql

我遇到一个关于mysql表显示结果的问题。我的MySql数据库中有一个这样的表。

表名 - 书籍

+----+--------------------------+--------+-------+-------+----------------+
| 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        |
+----+--------------------------+--------+-------+-------+----------------+

我想显示如下。

+---------------------+---------------+-------+-----------+
|     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 |
+------------------------+-------+-------+---------+

我需要php和mysql代码。感谢

1 个答案:

答案 0 :(得分:1)

您似乎只需要按作者订购数据,但是没有按顺序排列它们的顺序:

  • 亨利
  • 散聚
  • 佛瑞德
  • 标记

因为这不是按字母顺序递增还是降序?

以下查询将使您能够按作者ASC / DESC订购:

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

但是,如果您需要以这种奇怪的顺序输出,您可能需要逐个检索每位作者的书籍并逐一输出:

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

然而,我不会发布如何从数据库中检索数据,因为您可以阅读有关PHP / MySQL代码帮助的在线教程。请参阅以下链接以获取友好的PDO参考:

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

希望这会有所帮助 - 抱歉,我无法提供更多帮助。

古德勒克!