473,699 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stupid Query-thinking?

Hi there,

I'm building a website about animals.
There is a mySQL DB behind it.
There are 2 tables: 1 - species
2 - animals

Species could be: birds, fishes etc.
Animals would be: hawk, eagle etc, and orca, whale etc.
So every animal belongs to one of the species.

Anyway, there is an admin part to delete or edit animals or entire
species.

You get an overview of all the species, and behin it is a number of
how much animals there are in it.

I calculated them as following:

(Query to get species){
$species_name=$ query['name'];
$count_animals = mysql_query("SE LECT COUNT(*) FROM animals WHERE
sort_of_species =$species_name" );
$total_animals_ in_species = mysql_result($c ount_total_item s, 0);
echo $species_name." amount=".$total _animals_in_spe cies;
}

To make a long story short, for each positive result in the
"get-species-query" i run another one to count the animals inside.

is this heavy for the server/DB, is there a better way to do this?

Sorry for the long story, i hope it's clear...

Thanks in advance.

greetings knoakske
Jul 17 '05 #1
4 1592
knoak wrote:
Hi there,

I'm building a website about animals.
There is a mySQL DB behind it.
There are 2 tables: 1 - species
2 - animals

Species could be: birds, fishes etc.
Animals would be: hawk, eagle etc, and orca, whale etc.
So every animal belongs to one of the species.

Anyway, there is an admin part to delete or edit animals or entire
species.

You get an overview of all the species, and behin it is a number of
how much animals there are in it.

I calculated them as following:

(Query to get species){
$species_name=$ query['name'];
$count_animals = mysql_query("SE LECT COUNT(*) FROM animals WHERE
sort_of_species =$species_name" );
$total_animals_ in_species = mysql_result($c ount_total_item s, 0);
echo $species_name." amount=".$total _animals_in_spe cies;
}

To make a long story short, for each positive result in the
"get-species-query" i run another one to count the animals inside.

is this heavy for the server/DB, is there a better way to do this?
sort_of_species should be an index. (Probably using a numeric ID is
faster than a varchar).
Sorry for the long story, i hope it's clear...


MySQL? Use explain your query here

read documentation on explain.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #2
knoak wrote:
Hi there,

I'm building a website about animals.
There is a mySQL DB behind it.
There are 2 tables: 1 - species
2 - animals

Species could be: birds, fishes etc.
Animals would be: hawk, eagle etc, and orca, whale etc.
So every animal belongs to one of the species.

Anyway, there is an admin part to delete or edit animals or entire
species.

You get an overview of all the species, and behin it is a number of
how much animals there are in it.


assuming the following tables in the db:

mysql> select * from species;
+------------+---------+
| species_id | species |
+------------+---------+
| 1 | birds |
| 2 | fishes |
+------------+---------+

mysql> select * from animal;
+-----------+--------+------------+
| animal_id | animal | species_id |
+-----------+--------+------------+
| 1 | hawk | 1 |
| 2 | eagle | 1 |
| 3 | orca | 2 |
| 4 | whale | 2 |
+-----------+--------+------------+
You can get your numbers in one pass with this query:

mysql> select species, count(animal.sp ecies_id) as 'number of animals'
from species, animal where species.species _id=animal.spec ies_id group by
species;
+---------+-------------------+
| species | number of animals |
+---------+-------------------+
| birds | 2 |
| fishes | 2 |
+---------+-------------------+

HTH,
JP

--
Sorry, <de*****@cauce. org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #3
knoak wrote:
I'm building a website about animals.
There is a mySQL DB behind it.
There are 2 tables: 1 - species
2 - animals

Species could be: birds, fishes etc.
Animals would be: hawk, eagle etc, and orca, whale etc.
SCNR ... orca and whale are fish? :-)
So every animal belongs to one of the species.

Anyway, there is an admin part to delete or edit animals or entire
species.

You get an overview of all the species, and behin it is a number of
how much animals there are in it.
It all depends on how your tables are defined.

With these definitions:

mysql> desc species;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| name | varchar(20) | | | | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> desc animal;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| name | varchar(20) | | | | |
| species_id | int(11) | | | | |
+------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

I'd first get only the species' names and how many animals each has:

mysql> select s.name, count(a.id)
-> from species s left join animal a on a.species_id=s. id
-> group by s.name;
+--------+-------------+
| name | count(a.id) |
+--------+-------------+
| bird | 2 |
| fish | 0 |
| mammal | 2 |
+--------+-------------+
3 rows in set (0.00 sec)

and show that on the first selection. When the admin selects a species,
get its animals:

mysql> select a.name
-> from animal a, species s
-> where species_id=s.id
-> and s.name='mammal' ; -- or s.id=3
+-------+
| name |
+-------+
| orca |
| whale |
+-------+
2 rows in set (0.00 sec)

is this heavy for the server/DB, is there a better way to do this?


As much as possible each query should ask exactly what you want and no
more. Always try to do a maximum of one single mysql_query() call per
php page.

Page 1: species -> get the species' names and number of animals in *one*
query

Page 2: animals from a species (passed in $_GET array) -> get just the
animals from the specified species.

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #4
Hi knoak,

This question gets only interesting if species contains a hierachy,
like:
animals
birds
birds of pray
hawks
eagles
fishes
mamals
sea mamals
orca's
whales

The hierarchy uses a recursive foreign key equal to the key of the more
generic species group (which is in the same table as the more specific
ones).

Suppose the system is used by a field biologist, there may be another
table holding the numbers of the rings and tags, together with some
descriptive data like date of tagging, gender, coloring, size etc., and
of course a foreign key equal to the key of the most exact species the
tagged animal belongs to. Now we want to know the number of a higer
species group (for example birds) that where tagged after a certain
date. The code should work for a hierarchy of species of arbitrary depth
and any species (group). You can use any combination of SQL and php
code, but it should still perform reasonbly on a single processor off
the shelve server for about 10.000 species in an up to 10 deep
hierarchy. You can add some recursion into the data structure, but not
store the total number of tags per species group in the database.

Have fun!
Henk Verhoeven,
MetaClass.
knoak wrote:
Hi there,

I'm building a website about animals.
There is a mySQL DB behind it.
There are 2 tables: 1 - species
2 - animals

Species could be: birds, fishes etc.
Animals would be: hawk, eagle etc, and orca, whale etc.
So every animal belongs to one of the species.

Anyway, there is an admin part to delete or edit animals or entire
species.

You get an overview of all the species, and behin it is a number of
how much animals there are in it.

I calculated them as following:

(Query to get species){
$species_name=$ query['name'];
$count_animals = mysql_query("SE LECT COUNT(*) FROM animals WHERE
sort_of_species =$species_name" );
$total_animals_ in_species = mysql_result($c ount_total_item s, 0);
echo $species_name." amount=".$total _animals_in_spe cies;
}

To make a long story short, for each positive result in the
"get-species-query" i run another one to count the animals inside.

is this heavy for the server/DB, is there a better way to do this?

Sorry for the long story, i hope it's clear...

Thanks in advance.

greetings knoakske

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1821
by: TKD Karen | last post by:
I wrote this function (below) that is called 3 times; one for each case scenario. But the function only works for the Unresolved, not for the other 2 cases. There doesn't seem to be anything wrong with the strSQL statement because if I put it into an Access query design, I get results for all 3 values. But when I run it (unresolved is the last one called, btw), Admin and Fraud are coming up Null. Any ideas what the problem is? ...
16
2076
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables: 'tblContact' and 'tblCategory' where categories are like: Code Name 010101 Short 010102 Fat
9
1568
by: noone | last post by:
I have a database file that I use an autonumber field as the primary key index. Because of some rearrangements in the past, this index does not match the order that I would like it to be in, that is date order. What I would like to do is to eliminated the primary key field (I know how to do this), sort on the date field, and then reindex with a new autonumber field. Every time I try, however, the default sort field left over from the...
4
1351
by: Steven Steyaert | last post by:
I hided a Query (via properties), how to unhide?? Is there another simple way to protect a query so that nobody can run it? Thx in advance Steven
2
2304
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int word=0; char *querystring; querystring=malloc(sizeof(char)*100000); if (getenv("QUERY_STRING")==NULL)
2
1069
by: Justin Rich | last post by:
i have my form (default.aspx) and then my logic (default.aspx.cs) and when the logic is done, i want it to go to another page to display the results (results.aspx)... i dont really want to have to clear or hide my form on the default.aspx page... i need to pass a few control values to a new page and just display them. i assume this is easy, and stupid but i couldnt locate it.
3
1413
by: MadCrazyNewbie | last post by:
Hey Group, Probably a Stupid question i know but, what is the difference between a datagrid and Dataview? Ta MCN
4
1652
by: JimmyHoffa | last post by:
Hi hoping someone might be able to help with this issue, The following query is one i'd love to be able to use, but cant... select nodeid,max(datetimestamp) as datetimestamp,latitude,longitude from trackinglogs group by nodeid order by datetimestamp desc; I'm trying to get the latest rows according to datetimestamp column,
1
1185
keyvanrahmadi
by: keyvanrahmadi | last post by:
I have created a form which is as Follow: <form action="fleet-of-car.php" method="post"> <br />SELECT A FORM TO SEE : <select name="FORM"> <option value="ALL CARS"/>All Cars <Option Value="CARS BY MANUFACTURER"/>CARS BY MANUFACTURER <Option Value="CARS BY FUEL TYPE"/>CARS BY FUEL TYPE
0
980
by: webandwe | last post by:
Hi, When I press sign(submit) - It looks like something is wrong and the code does not want to insert the info into my SQL...I get the error message "could not query.." that I type in under "#create the sql query" I went trough the book I got the script from and check to see that all of the spaces and letter is exactly as it should be..I am still learning php and figure I made a stupid mistake that problem someone that is fluent in PHP will...
0
8691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9180
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8920
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6536
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.