473,401 Members | 2,068 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,401 software developers and data experts.

mysql counting occurances in a table

Hi,

Here is a table that I am working with:

PERSON | HAS
--------------------------------------------------------
John | Laptop
John | Cell Phone
John | PS3
Sally | Cell Phone
Sally | Laptop
Jane | Laptop


I want my query to return the set of people who own all 3 items (Laptop, Cell Phone, PS3)....

So in this example above, the only name that should come up is "John"

One catch, this has to be dynamic, so future items can be added to the list. Basically, it's suppose to count all items in the list and see if a person owns all of them.

Please help.
Nov 1 '09 #1
4 2157
RedSon
5,000 Expert 4TB
Check out the count() function.
Nov 2 '09 #2
Expand|Select|Wrap|Line Numbers
  1. SELECT person
  2. FROM my_table
  3. GROUP BY person
  4. HAVING Count( person ) = Count( has )
this is what i have right now and no luck.
Nov 2 '09 #3
RedSon
5,000 Expert 4TB
try something like :

count (has) from my_table where person = john.
Nov 3 '09 #4
SLauren
60
You can try something like:

Expand|Select|Wrap|Line Numbers
  1. SELECT person FROM my_table GROUP BY person HAVING COUNT(has) = (SELECT COUNT(DISTINCT has) FROM my_table);

Hope this could help.

Thanks,
Lauren
Nov 3 '09 #5

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

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
6
by: PG | last post by:
When deleting a row from the database, that id is now missing. So what I'm trying to do is update in a loop (maybe an sql loop if there is one) of all of the id numbers - sort of like renaming...
6
by: jabailo | last post by:
Which would be faster for counting lines in a StreamReader: (a) iterate through a file using .ReadLine() and adding to a counter, i++ (b) doing a .ReadToEnd() and then using an IndexOf() method...
2
by: Peter | last post by:
What is the most efficient way to count occurances of charaters in a string? IE. Searching for ',' in a comma del file.
2
by: Jason Barnett | last post by:
I thought I had seen a method for returning the number of occurances of a substring within a string object, but I can't seem to find it now. Does anyone know of something? I know I can parse...
1
by: jenson | last post by:
<?php /* * Created on Apr 13, 2007 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once 'init.php'; ...
20
by: cmbcorp | last post by:
Hi, I have been playing around with a simple php login script and im getting an error message when i attempt to log in with the username and password i set in the sql table. The error message is...
4
by: fisherd | last post by:
When i run this code, i keep getting this message; Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\checklogin.php on line 26 i use this code to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.