Let’s say i have a database design like this:
for the data:
id | title | category
1 | first title | 1
2 | second title | 4
3 | third title | 5
4 | fourth title | 2
5 | fifth title | 3
6 | other title | 1
and for the categories:
id | name
1 | music
2 | movies
3 | funny
4 | people
5 | sport
How can i output a list with each category with item title from that category?
somethink like:
<div>
<h1>music</h1>
<div>first title</div>
<div>other title</div>
<div>
<div>
<h1>movies</h1>
<div>fourth title</div>
<div>
// and so on...
I don’t know how to formulate the mysql query for a result like this…
Can somebody please advise me? (i hope the example can be understood easily)
Thanks in advance.
EDIT:
i used Eugen Rieck’s code but i don’t know how to do the following thing
how can i order by the categories.id and titles.id in the same time eventually use a where condition after that?
something like this: ORDER BY titles.id DESC LIMIT 0, 10 WHERE titles.views > 10
Assuming your first table is called
titlesand your second tablecategories, use a query likeand then do a group change loop
EDIT
I updated the above code to limit the count of titles displayed per category to
MAX_TITLES_PER_CAT– please understand, that this is very inefficient, if the average number of titles per category is much higher thanMAX_TITLES_PER_CAT.