2010-07-06 6 views
1

merci pour loking.php mysql afficher par vues

i ont 3 tables

livres, book_category, book_click.

livres Table book_id, url, titre , img, short_desc, en vedette, permettent

Table book_category book_id, category_id

book_clicks Table site_id , temps, points de vue

j'aime obtenir des données de livres tableau par vue SUMS mais cela ne les rendements sur un résultat

SELECT books.book_id, url, title, short_desc, img, featured, SUM(views) as total_views 
FROM books,book_category,book_click WHERE enable=1 AND category_id=7 
AND books.book_id=book_category.book_id 
ORDER BY strong texttotal_views DESC 
+0

Vous devez poster le schéma, parce que, pour moi au moins, la requête ne veut rien dire sans la structure de la table. –

+0

Vous n'avez aucun lien entre les vues et les livres eux-mêmes. Comment savez-vous quelle entrée dans la table book_clicks va à quel livre? – Joseph

Répondre

1

Commander le mySQL ROLLUP

MISE À JOUR

Après la lecture votre quesion à nouveau, il pourrait être aussi simple que d'ajouter GROUP BY books.book_id

SELECT books.book_id, url, title, short_desc, img, featured, SUM(views) as total_views 
FROM books,book_category,book_click WHERE enable=1 AND category_id=7 
AND books.book_id=book_category.book_id 
ORDER BY strong texttotal_views DESC GROUP BY books.book_id 

Réponse d'origine:

http://dev.mysql.com/doc/refman/5.1/en/group-by-modifiers.html

SELECT books.book_id, url, title, short_desc, img, featured, SUM(views) as total_views 
FROM books,book_category,book_click WHERE enable=1 AND category_id=7 
AND books.book_id=book_category.book_id 
ORDER BY strong texttotal_views DESC GROUP BY your_grouping WITH ROLLUP; 

Exemple

mysql> SELECT year, country, product, SUM(profit) 
    -> FROM sales 
    -> GROUP BY year, country, product WITH ROLLUP; 
+------+---------+------------+-------------+ 
| year | country | product | SUM(profit) | 
+------+---------+------------+-------------+ 
| 2000 | Finland | Computer |  1500 | 
| 2000 | Finland | Phone  |   100 | 
| 2000 | Finland | NULL  |  1600 | 
| 2000 | India | Calculator |   150 | 
| 2000 | India | Computer |  1200 | 
| 2000 | India | NULL  |  1350 | 
+0

merci mais pas workin get total_views 79 pour tous les résultats – jay

+0

merci j'ai fait quelques ajustements mineurs et cela fonctionne. – jay

Questions connexes