开发者

mysql querys show row type once

开发者 https://www.devze.com 2023-02-06 07:06 出处:网络
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 pag

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`
0

精彩评论

暂无评论...
验证码 换一张
取 消