473,395 Members | 1,495 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,395 software developers and data experts.

Searching Multiple Tables

I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:

1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)

2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)

3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)

I am not free to adjust the tables, so I have to stick with what is
there. There may be an SQL statement that could do all this in one
command, but I am no SQL expert. I was wondering if anyone had some
PHP oriented solutions. I can get the show_id, no problem. I seem to
be having a problem with the rest, as it involves arrays. After I go
into table2 and get all the attendee_ids, I get an array of
attendee_ids that I need to search table3 for, to create a new array
of attendee names.

Aug 7 '07 #1
5 1478
Jerim79 wrote:
I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:

1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)

2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)

3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)

I am not free to adjust the tables, so I have to stick with what is
there. There may be an SQL statement that could do all this in one
command, but I am no SQL expert.
select
table3.attendee_name
from
table1, table2, table3
where
table1.show_name = 'Mickey Mouse' and
table1.show_id = table2.show_id and
table2.attendee_id = table3.attendee_id

Note that the default String enclosing characters in MySQL are not
standard, i.e., not '', so you have to change that.

Aug 7 '07 #2
Jerim79 wrote:
I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:

1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)

2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)

3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)

I am not free to adjust the tables, so I have to stick with what is
there.
Hi,

So far it sounds like a nice denormalized database.
Please don't 'adjust' it. ;-)
I think your database is designed just fine (based on what you told).

There may be an SQL statement that could do all this in one
command, but I am no SQL expert.
Yes, that is easy done in one SQL statement that joins the 3 tables.

I was wondering if anyone had some
PHP oriented solutions. I can get the show_id, no problem. I seem to
be having a problem with the rest, as it involves arrays. After I go
into table2 and get all the attendee_ids, I get an array of
attendee_ids that I need to search table3 for, to create a new array
of attendee names.
Don't.
Write a single query that joins the 3 tables and let the database do the
hard work.

You might want to throw in the 3 relevant tables (how they are
structured) if you want us to make the join.
(Or better, repost that to a databasenewsgroup)

Good luck.

Regards,
Erwin Moller
Aug 7 '07 #3
Jerim79 wrote:
I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:

1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)

2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)

3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)

I am not free to adjust the tables, so I have to stick with what is
there.
Hi,

So far it sounds like a nice normalized database.
Please don't 'adjust' it. ;-)
I think your database is designed just fine (based on what you told).

There may be an SQL statement that could do all this in one
command, but I am no SQL expert.
Yes, that is easy done in one SQL statement that joins the 3 tables.

I was wondering if anyone had some
PHP oriented solutions. I can get the show_id, no problem. I seem to
be having a problem with the rest, as it involves arrays. After I go
into table2 and get all the attendee_ids, I get an array of
attendee_ids that I need to search table3 for, to create a new array
of attendee names.
Don't.
Write a single query that joins the 3 tables and let the database do the
hard work.

You might want to throw in the 3 relevant tables (how they are
structured) if you want us to make the join.
(Or better, repost that to a databasenewsgroup)

Good luck.

Regards,
Erwin Moller
Aug 7 '07 #4
On Aug 7, 10:08 am, Jerim79 <my...@hotmail.comwrote:
I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:

1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)

2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)

3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)

I am not free to adjust the tables, so I have to stick with what is
there. There may be an SQL statement that could do all this in one
command, but I am no SQL expert. I was wondering if anyone had some
PHP oriented solutions. I can get the show_id, no problem. I seem to
be having a problem with the rest, as it involves arrays. After I go
into table2 and get all the attendee_ids, I get an array of
attendee_ids that I need to search table3 for, to create a new array
of attendee names.

$row_rsIDAttend = mysql_fetch_assoc($result_from_atendee_id_sql); //
result from sql request of tbl2
do {

$qry_rsAttendee = "SELECT name FROM table3 WHERE id = " .
$row_rsAttendee['attendee_id']; //sql tbl3
$result = mysql_query($qry_rsAttendee); // run query
$row_rsAttendee = mysql_fetch_assoc($result); //fetch the first/next
array of field data
$array_attendee_names[] = $row_rsAttendee['name']; //fill your data
array of names

}while ($row_rsIDAttend =
mysql_fetch_assoc($result_from_atendee_id_sql));
I wrote this out pretty quick, so check over my syntax, but this
should do what you want.
once the do loop is finished you will have an array
($array_attendee_names[]) with the number of elements in it that
represent your attendee list. Each individual element (attendee name)
can be accessed by refering to an element in the array
($array_attendee_names[1]). The elements start at 0. so if there are
5 attendees they will be numbered 0-4. If you want to show the 3rd
attendee in the list do this:

echo $array_attendee_names[2];

check out this for more on arrays:

http://us2.php.net/manual/en/language.types.array.php - basic array
use info

http://us2.php.net/manual/en/ref.array.php - array functions

GJ

Aug 7 '07 #5
On Aug 7, 9:26 am, Boris Stumm <st...@informatik.uni-kl.dewrote:
Jerim79 wrote:
I am trying to create a search system for an existing database.
Because of the way the database is setup, I need to traverse three
different tables, gathering information:
1) Go into table1 and lookup show_id by show name. (For instance,
let's say I am looking for the Mickey Mouse show. I look it up and
find that it's show_id is 6)
2) Go into table2 and lookup all attendee_ids for show_id 6. (For
instance, I lookup show_id 6 and find 5 attendees. 1, 2, 3, 4 and 5
are the attendee_id's)
3) Go into table3 and lookup attendee names, from the 5 attendee_ids.
(For instance, I lookup 1, 2, 3, 4 and 5 to find their names are Jane,
Alice, Tom, Peter and Greg.)
I am not free to adjust the tables, so I have to stick with what is
there. There may be an SQL statement that could do all this in one
command, but I am no SQL expert.

select
table3.attendee_name
from
table1, table2, table3
where
table1.show_name = 'Mickey Mouse' and
table1.show_id = table2.show_id and
table2.attendee_id = table3.attendee_id

Note that the default String enclosing characters in MySQL are not
standard, i.e., not '', so you have to change that.
That worked like a charm. Thank you very much. I took SQL back in
college, and use it from time to time, but never really have a need
for it 90% of the time. Your example immediately made sense.

Aug 7 '07 #6

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

Similar topics

0
by: Ralph Guzman | last post by:
I have to do a catalog search through multiple tables and columns for product model number, description, and name. I realize that doing pattern matching with multiple LIKE statements is slow so I...
0
by: michael calwell | last post by:
Dear all, I have googled until my eyed have fallen out, and still I cannot find what I am looking for, and I would be grateful for any help at all. I can redesign my tables, create new fields...
1
by: Robert Oschler | last post by:
I read a while back that MySQL will only use one index per query. (If this is not so, please tell me and point me to a doc that gives a good explanation of MySQL's current index usage policy). ...
6
by: calan | last post by:
Relative SQL newbie here......this is probably easy, but.... Lets say I have a table (MainTable) that stores a list of input table names, a primary key (PKey), and a field called "Configured" for...
3
by: Katrina | last post by:
I am trying to write a piece of code that will search through a number of different tables (current one being tableNm) to look for a specific street name that has been entered by the user and saved...
5
by: David Wender | last post by:
I want to create a dataview with a sort on multiple columns. However, when I use FindRows, I only want to search some of the columns, not all. Is this possible? I have not been able to make it...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
0
by: qudra | last post by:
Searching multiple records -------------------------------------------------------------------------------- Hi guys. I need ur help. In VB.NET we usually use this code to search the database...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.