473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Counting rows

Hello!
I have two tables; one containing product names and one with reviews of the products. What I'd like to do is present a table with the product names and number of reviews. The thing is that there can be more than one entry of a certain product in the products table. When I write something like:
SELECT products.name, COUNT(reviews.review)
FROM products
JOIN reviews ON products.name = reviews.productname
GROUP BY products.name
I will get a table like:

ProductA 3
ProductA 3
ProductB 1
ProductC 0

In this case ProductA appears twice in the products table and has 3 reviews in the reviews table. But I just want it to appear once in the joined table, how to do? Any tip highly appreciated.

/Chris
Apr 5 '08 #1
10 1125
Try LEFT OUTER JOIN instead of simple join.
Apr 5 '08 #2
ck9663
2,878 Expert 2GB
Hello!
I have two tables; one containing product names and one with reviews of the products. What I'd like to do is present a table with the product names and number of reviews. The thing is that there can be more than one entry of a certain product in the products table. When I write something like:
SELECT products.name, COUNT(reviews.review)
FROM products
JOIN reviews ON products.name = reviews.productname
GROUP BY products.name
I will get a table like:

ProductA 3
ProductA 3
ProductB 1
ProductC 0

In this case ProductA appears twice in the products table and has 3 reviews in the reviews table. But I just want it to appear once in the joined table, how to do? Any tip highly appreciated.

/Chris
Is there a product table that contains some sort of product id that is unique ?

-- CK
Apr 7 '08 #3
balame2004
142 100+
Hi,

Try this:

SELECT distinct products.name, COUNT(reviews.review)
FROM products
JOIN reviews ON products.name = reviews.productname
GROUP BY products.name


Balaji U
Apr 7 '08 #4
Is there a product table that contains some sort of product id that is unique ?

-- CK
Yes, each entry has a guid like so:
GUID Name
==== ====
0x01 ProductA
0x02 ProductB
0x03 ProductA
0x04 ProductC

/Chris
Apr 7 '08 #5
Hi,

Try this:

SELECT distinct products.name, COUNT(reviews.review)
FROM products
JOIN reviews ON products.name = reviews.productname
GROUP BY products.name


Balaji U
Doesn't matter if I use DISTINCT since I'm grouping by product name.
If I have product table like this:
Name
=======
ProductA
ProductA
ProductA
ProductB
ProductC

...and a review table like this:
Prod_name Review
======== =====
ProductA bla bla
ProductA bla bla
ProductB bla bla

The joined table will be:
Product No. of reviews
======== ==========
ProductA 6
ProductB 1
ProductC 0

/Chris
Apr 7 '08 #6
Try LEFT OUTER JOIN instead of simple join.
Doesn't work either, see post above.

/Chris
Apr 7 '08 #7
ck9663
2,878 Expert 2GB
If PRODUCTNAME is also in reviews, why not just query the REVIEWS table?

Expand|Select|Wrap|Line Numbers
  1. SELECT reviews.name, COUNT(*) as cnt
  2. FROM reviews 
  3. GROUP BY reviews.name
if you just want to make sure if it's existing in PRODUCTS table just do a WHERE IN condition...

-- CK
Apr 7 '08 #8
If PRODUCTNAME is also in reviews, why not just query the REVIEWS table?

Expand|Select|Wrap|Line Numbers
  1. SELECT reviews.name, COUNT(*) as cnt
  2. FROM reviews 
  3. GROUP BY reviews.name
if you just want to make sure if it's existing in PRODUCTS table just do a WHERE IN condition...

-- CK
Because then the products that have zero reviews won't be listed and I need to include them as well.

/Chris
Apr 8 '08 #9
ck9663
2,878 Expert 2GB
You should consider using GUID as key. For now am following your requirement, that you're using the name as key. Try:


Expand|Select|Wrap|Line Numbers
  1. SELECT products.name, COUNT(reviews.review)
  2. FROM (select distinct name from products) products
  3. JOIN reviews ON products.name = reviews.productname
  4. GROUP BY products.name
-- CK
Apr 8 '08 #10
You should consider using GUID as key. For now am following your requirement, that you're using the name as key. Try:


Expand|Select|Wrap|Line Numbers
  1. SELECT products.name, COUNT(reviews.review)
  2. FROM (select distinct name from products) products
  3. JOIN reviews ON products.name = reviews.productname
  4. GROUP BY products.name
-- CK
Using FULL JOIN, worked like a charm! Thanks a lot!

/Chris
Apr 8 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
16
by: walexand | last post by:
I use the database mysql v.4. My problem is... I have a select like: select * from user where language = "de"; the result are then: id name =================== 1 max
3
by: Viswanatha Thalakola | last post by:
Hello, Can someone point me to getting the total number of inserts and updates on a table over a period of time? I just want to measure the insert and update activity on the tables. Thanks....
2
by: Adam | last post by:
In my MYISAM table I have an index (Index_A) on 2 fields (Field_A, Field_B). There are millions of rows in the table. The cardinality of Index_A is 53. I think a query to count the number of rows...
3
by: Peter | last post by:
Hi, This post is about counting rows and finding the max count in a group of rows. I'm trying to find the player who scored the most goals in a game. I know how to find out how many goals the...
0
by: Chia Lee Lee | last post by:
Hello… I have problem when counting the number of records, which is based on the start date and end date. I have tried to use message box to prompt the result, but the result given is...
1
by: Newmanbt | last post by:
I've got the following code testlabel.Text = allquestions.Select().Count.ToString(); GridView1.DataSource = allquestions.Select(); GridView1.DataBind(); testlabel.Text =...
1
by: jasone | last post by:
hey, this is what ive got so far: ("SELECT (Select count(*) from tbl_flight_details) + (select count(*) FROM tbl_flight_departures) as grandtotal") i need to count all the records in the...
2
by: akadeco | last post by:
Hi I need to write a script that will allow me to check the value of any link in a table cell. The desired result would be something like this: row.cell.innerHTML, allowing me to call the contents...
7
by: crochunter | last post by:
Hi I was trying to count rows and columns in a tab delimited flat file. Like here in example below i should be able to detect automatically the no of rows and columns. So I should get 5 columns...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.