469,287 Members | 2,767 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

MySQL query on multiple tables ?

Jay
Hi everyone !

I hope someone will be able to help me with this problem.
I currently have several tables in MySQL database. Below is my table
structures.

(Data type for each fields are ignored)

user
{
uid,
firstname,
lastname,
date_added,
}

contact
{
uid, //same as "uid" in "user" table.
e-mail,
homephone,
workphone,
cellphone,
address,
city,
state,
zipcode
}
//each field in the classes table contains a grade that the user
(student) got form
//taking that class.
classes
{
uid //same id as "uid" in the "user" table.
political100,
history101,
bio109,
.....
....etc...

}

My goals include the following things:

1. Search for users(students) in a particular city/state/zipcode that
completed a certain
classes (let's say bio109 with an "A" OR history 101 with a "B" or a
combination of all classes with
different combination of "grades". How would I write a "SELECT"
statement that would accomplish this task ?

2. Please note that table contact and classes have a Primary keys that
are not "auto-increment"
The primary keys are from table "user".
I am sorry for such a lengthy message.

Any help would be appreciated ! Thank you in advance,

Jay Hana

Jul 17 '05 #1
7 10008
2nd question first:

imho you table design is flawed. what if a student has two adresses?
you'd need two entries with the same uid which is not possible if uid
is a primary key.
two ways out of that:
1. if you are SURE there's only one adress per student, than there's no
need for the second table (not good i think)
2. and better: introduce an auto increment primary key to all tables,
and use uid as a foreign key in the 2nd and 3rd table

i'd even propose another table 'subjects' where to keep the subject
names and have their id' in the classes tables.

best if read something abiout normalizing db structures, i.e.
here:http://pubs.logicalexpressions.com/P...icle.asp?ID=88

1st question: you need JOINS for that, but i'm not gonna writ an sql
statement down now, because you should fix your data structure first

micha

Jul 17 '05 #2
I noticed that Message-ID:
<11**********************@f14g2000cwb.googlegroups .com> from Jay
contained the following:

2. Please note that table contact and classes have a Primary keys that
are not "auto-increment"
The primary keys are from table "user".


Then there seems very little point in having three tables.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
Jay wrote:
Hi everyone !

I hope someone will be able to help me with this problem.
I currently have several tables in MySQL database. Below is my table
structures.

(Data type for each fields are ignored)

user
{
uid,
firstname,
lastname,
date_added,
}

contact
{
uid, //same as "uid" in "user" table.
e-mail,
homephone,
workphone,
cellphone,
address,
city,
state,
zipcode
}
//each field in the classes table contains a grade that the user
(student) got form
//taking that class.
classes
{
uid //same id as "uid" in the "user" table.
political100,
history101,
bio109,
.....
....etc...

}

My goals include the following things:

1. Search for users(students) in a particular city/state/zipcode that
completed a certain
classes (let's say bio109 with an "A" OR history 101 with a "B" or a
combination of all classes with
different combination of "grades". How would I write a "SELECT"
statement that would accomplish this task ?

2. Please note that table contact and classes have a Primary keys that
are not "auto-increment"
The primary keys are from table "user".
I am sorry for such a lengthy message.

Any help would be appreciated ! Thank you in advance,

Jay Hana


Jay,

I agree with Micha. You should look into normalizing your database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #4
Thanks everybody for your input.

OK, here are what I have done for my problem.

I still keep the "id" in tables "contact" and "classes" and make these
"id" fields primary keys in the tables they reside in. Would that solve
the database structure problem ?

If it solves the problem, then how would I make the "select" query on
multiple tables ?

Thanks again,

Jay Hana

Jul 17 '05 #5
I noticed that Message-ID:
<11**********************@g14g2000cwa.googlegroups .com> from John
contained the following:
I still keep the "id" in tables "contact" and "classes" and make these
"id" fields primary keys in the tables they reside in. Would that solve
the database structure problem ?


No.

Your data is not structured at the moment, it is simply split across
three tables.

I'm not an SQL expert but you could probably do

SELECT DISTINCT col1,col2,col3... FROM tbl1,tbl2,tbl3 WHERE
tbl1.id=tbl2.id AND tbl2.id=tbl3.id AND (bio109 ='A' OR history101='B')

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
I'm not an SQL expert but you could probably do

SELECT DISTINCT col1,col2,col3... FROM tbl1,tbl2,tbl3 WHERE
tbl1.id=tbl2.id AND tbl2.id=tbl3.id AND (bio109 ='A' OR history101='B')


You beat me to it. This is basic SQL, and has nothing to do with PHP. I
just had to do one for myself where I did:

select a.*, b.field1 from a, b where a.id=b.id;

Shelly
Jul 17 '05 #7
it would not solve you databse problem.

the basic point is that:
a table should have a primary key as a means to identify without any
doubt any given record in that table. up to here this is totally
independend of any other table in that database.

but now the strong point of a database is that it can link records in
different tables with each other. here you use primary/foreign key
pairs, i.e. primary key of table_user becomes foreign key in
table_contact. that way it's possible to have, say, user 1 'owning'
contact records 7 and 25 (by having 1 as the foreign key in that
records) - means having two adresses. now if user decides to change
his/her name, this will not affect his adresses, you change the name in
the master record once and the rest will still be up to date.

a good way to do that is to find, well, let's call it independent data
objects: in your case something like that:
an user and his login
an user and his adress (couple of records per user)
a class
a user and his classe (couple of records per user)
the whole process of identifiying which data to put in which table and
how to link them is called 'normalizing'. i strongly advice you to have
at least some basic knowledge of that before desingning a database,
even for a small application, because they tend to get bigger and
bigger over time, and if the data is not normalized, you WILL reach the
point where your data structure cannot contain the needed data anymore.
you did in fact reach that point already: your data structure does not
allow for several user's adresses.

micha

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Lenz Grimmer | last post: by
6 posts views Thread by jacob nikom | last post: by
reply views Thread by Mike Chirico | last post: by
33 posts views Thread by Joshua D. Drake | last post: by
39 posts views Thread by Mairhtin O'Feannag | last post: by
3 posts views Thread by cdelarte | last post: by
Atli
6 posts views Thread by Atli | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.