NYCPHP Meetup

NYPHP.org

[nycphp-talk] MySQL Query Question

harvey list at harveyk.com
Sun Dec 19 19:12:17 EST 2004


Hello NYPHP,

Let's say that I have a db and it has a table with list of Senators. Each 
Senator record has a field for state_fid. Then there's a States table with 
the names of the states and their associated state_id's. The Senator table 
also has a field for party_fid. And there's a Party table with the names of 
the parties and their associated party_id's.

Now suppose I want to query the db and alphabetically list the Name, State, 
and Party of each Senator. Question is this--in general, which (if either) 
is the preferred method?

Create one query similar to:
Query Senator
Select senate.senator_name, state.state_name, party.party_name
 From senate, state, party
Where senate.state_fid = state.state_id
And senate.party_fid = party.party_id
Order by senate.senator_name

Then loop through the results to print.

Or create multiple queries similar to:
Select senator_name, state_fid, party_fid
 From senate
Order by senator_name

And then loop through the results with add'l queries similar to:
Query state
Select name from state where state_id = $row_Senator['state_fid']
...
Query party
Select name from party where party_id = $row_Senator['party_fid']
...

The first method would seem more elegant, but it seems to take longer also 
(although that might just be my imagination).

Any suggestions? Thanks!

Harvey





More information about the talk mailing list