Say I have 3 columns named 'page', 'book info' with various values one being 'author', the and 'book data' where I would have values such as 'dan brown', 'moliere'... I could have many pages with moliere being the author. My query is currently:
$data = mysql_query("SELECT * FROM table WHERE 'book info' = 'author'")
while($info = mysql_fetch_array( $data )) { echo $info['book data']; }
This lists all of the authors, as said earlier 'moliere' appears ma开发者_如何学JAVAny times, how would I only list an author once?
You can say to the RDBMS to return only distinct rows either with the DISTINCT keyword:
SELECT DISTINCT `book data` FROM table WHERE `book info` = 'author'
So MySQL will return only one row for each different book data
value.
Or with a GROUP:
SELECT `book data`, other_column FROM table WHERE `book info` = 'author' GROUP BY `book data`, other_column
You can use grouping:
SELECT * FROM table GROUP BY `book info`
which allows you for example to track the number of books written by each author:
SELECT count(`page`) FROM table GROUP BY `book info`
精彩评论