Re: Stupid Query-thinking?
From: Jan Pieter Kunst (devnull_at_cauce.org)
Date: 01/05/05
- Next message: News Me: "Re: Help with SQL statement"
- Previous message: skeeterbug: "php adodb data entry form question"
- In reply to: knoak: "Stupid Query-thinking?"
- Next in thread: Pedro Graca: "Re: Stupid Query-thinking?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 05 Jan 2005 21:35:22 +0100
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.species_id) as 'number of animals'
from species, animal where species.species_id=animal.species_id group by
species;
+---------+-------------------+
| species | number of animals |
+---------+-------------------+
| birds | 2 |
| fishes | 2 |
+---------+-------------------+
HTH,
JP
-- Sorry, <devnull@cauce.org> is a spam trap. Real e-mail address unavailable. 5000+ spams per month.
- Next message: News Me: "Re: Help with SQL statement"
- Previous message: skeeterbug: "php adodb data entry form question"
- In reply to: knoak: "Stupid Query-thinking?"
- Next in thread: Pedro Graca: "Re: Stupid Query-thinking?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|