Jump to content

[SOLVED] Interesting Query idea


SirChick

Recommended Posts

I was wondering if this can be done with a query.. lets say we have a table with a field called name.

 

So the list is:

 

Cheese

Bacon

Cheese

Cheese

Sausage

Bacon

Bacon

 

Now im trying to see if you can make a query so that the query will find only the name field where by there is less than 3 records with that name....

 

So in this particular situation if the query is possible only Sausage should appear:

 

1.Cheese

1.Bacon

2.Cheese

3.Cheese

1.Sausage

2.Bacon

3.Bacon

 

Can that be done =/ ive not made a query this complex before so worth asking first..cos I got no idea..

Link to comment
https://forums.phpfreaks.com/topic/73707-solved-interesting-query-idea/
Share on other sites

I gave it a try dont think i got it right lol

 

$CountNames = ("SELECT *, count(BusinessType) as C FROM `businesses` GROUP BY BusinessType HAVING C < 3");
$findbusinessresult = @mysql_query($CountNames) or die(mysql_error());

Echo $findbusinessresult;

 

No echo happens though =/

 

Just so you know on this the object is to find only the business types where by there are only 2 or less currently owned.

 

So if there was 3 hardware shops .. in the table.. that would suggest there are 3 already owned and so it would not show the field "businesstype" for that business.

<?php
$CountNames = ("SELECT *, count(BusinessType) as C FROM `businesses` GROUP BY BusinessType HAVING C < 3");
$findbusinessresult = @mysql_query($CountNames) or die(mysql_error());
$row = mysql_fetch_assoc($findbusinessresult); //<--missing
echo $row['BusinessType'];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.