Click to See Complete Forum and Search --> : best way to build a query


jason213123
July 1st, 2008, 06:36 AM
hi,
i have a table with all records that people say about a product for example

table:xxx
field: id , date, comment, product_owner_id

imagine that i have 100 records of comment for a product and 250 for other product and none for other product and now i want say that:

owner 1 have:
-product 1 have 100 records
-product 2 have 250 records
-product 3 have 0 records

owner 2 have:
-product 1 have 0 records
-product 2 have 0 records
-product 3 have 110 records

and so long, there is a way to build a simple query that donīt spend very resource of cpu? the db that i using is mysql

thanks a lot for your help

Scott.Macmaster
July 1st, 2008, 10:56 AM
Assuming id is the id the id of the product you could try something like

SELECT product_owner_id, Id, COUNT(*) AS CommentCount
FROM Comments
GROUP BY product_owner_id, Id

It won't show a count for products that have no comments. However, you could do a left outer join with the products tables to get those. Although, I think that would give you null instead of 0. However, you can easily change null to 0 if you'll using a program to display the results.

jason213123
July 2nd, 2008, 06:12 AM
hi,
thanks a lot for your help :)

i have more one question that may be this place is not the best...

i want save a value in real time using php/ajax/javascript/mysql
imagine this: you have a list of items and want check the items that you liked and them when you select/unselect the items they are saved in real time into mysql db i have see same like this working with cookies and not for all browsers i have see other way to do this not save but get the value without submit or reload the page any one know how can i do that?
the ideia is save a value of a checkbox for example when i check/uncheck them when i want consult the items that i have checked they will prompt a list that is saved in db

thanks for your help