Solved

how do I fix this COUNT query?

Posted on 2012-03-16
5
220 Views
Last Modified: 2012-04-11
I have four tables:

shops (shop,shop_id), products(product_id,date), sex(sex,product_id), users(user_id,prior_login)

shops.shop and shops.shop_id values are unique

I wish to COUNT the number of distinct shops for which at least one row in the products table exists such that shops.shop_id=products.shop_id, sex.product_id=products.product_id, products.date >= users.prior_login, users.user_id=22, and sex.sex=0.

Right now I am trying:

SELECT COUNT (shop_id) AS total_shops FROM shops
WHERE EXISTS (
SELECT * FROM
products, sex WHERE
shops.shop_id=products.shop_id AND
sex.product_id=products.product_id AND
sex.sex=0 AND
(products.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)))

Open in new window


This produces a syntax error. What am I doing wrong?
0
Comment
Question by:bitt3n
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:macarrillo1
ID: 37730701
Your not joining your tables correctly. But to provide the correct syntax, I need to know are you using MySQL or SQL Server?
0
 
LVL 9

Expert Comment

by:macarrillo1
ID: 37730777
If you have SQL Server you can try:


SELECT COUNT (shop_id) AS total_shops
FROM shops
WHERE EXISTS(
SELECT *
FROM shops s inner join products p
on s.shop_id=p.shop_id
inner join sex x
on p.product_id=x.product_id
WHERE
x.sex=0 AND
(p.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)  ) )
0
 

Author Comment

by:bitt3n
ID: 37730935
I am using MySQL. Looking at your query, I'm confused why it is not necessary to correlate 'shops s' in the EXISTS query with 'shops' in the outer query. (I tried using your query in MySQL but presumably there is some syntax difference between SQL Server and MySQL that prevents it from working.)
0
 

Author Comment

by:bitt3n
ID: 37731009
it appears the problem was merely the space in 'COUNT (shop_id)', as when I remove this space, I get the expected result.

I'll leave this question open in case anyone wishes to suggest some other correction or improvement, but I assume the problem was just this stupid syntax mistake.
0
 
LVL 9

Accepted Solution

by:
macarrillo1 earned 500 total points
ID: 37731012
Actually Shops is in the Exists query.  I am using an alias of 's'.  So instead of saying:
shops.shops_id
I can say
s.shops_id

Yes the syntax is different for MySLQ than for SQL Server.
 
Try:

SELECT COUNT (shop_id) AS total_shops
FROM shops
WHERE EXISTS (
SELECT *
FROM Products
Join Sex on  products.product_id=sex.product_id
Join Shops on products.shop_id=shops.shop_id
WHERE
sex.sex=0 AND
products.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)  
);
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now