PHP: MYSQL COUNT query with linked tables -
I'm exploring a cleaner way to do this. My code works, but I know that it can be better that I have three tables: with a category group list, one with category list, is linked to category groups, and one, in which the categories There is a list of news related to.
I need all the names of class groups, along with the name of the category named loop, category names, along with the number of news in each category listed.
I have three tables: category groups, categories, news.
I have a set of queries for the first query of all the rows from the category group's table:
$ result = mysql_query ('select cat_group_id, cat_group_name from the category group '); The second query is within the blocked results of the first query and searches for all categories that have news items and are related to specific category groups:
< Code> & lt ;? Php while ($ row = mysql_fetch_assoc ($ result)) {$ id = $ row ['cat_group_id']; $ Name = $ line ['cat_group_name']; Echo "& lt; h3 & gt; $ name & lt; / h3 & gt;"; $ Sql = mysql_query ("SELECT category_id", "Categories" by WHERE cat_group_id = $ id and category_id IN (select news categic_id from news) "); While ($ line = mysql_fetch_assoc ($ sql)) {$ title = $ line ['title']; $ Catid = $ row ['category_id']; $ Numbers = mysql_query ("select news from category_id = $ catid"); $ Nums = mysql_num_rows ($ numbers); Echo "$ heading ($ nums)
" \ n "; }? & Gt; I want to limit it to one or two questions, together with efficiency in mind. I know that this can be done, although I have not been successful in my efforts.
Thank you.
I recommend that you need to get a book on SQL, such as "."
$ sql = mysql_query ("Select cg.cat_group_name, c.title, COUNT '(category_ID) as' Category Group 'CG Join' category 'C UISing (Cat_Group_ID)' News 'Usaging (Category_ID) Group by CGCAT_Group_Name, Sitelit "); Then loop and output on the result is a new and <3> each time the cat_group_name is different from the previous row.
Comments
Post a Comment