473,387 Members | 3,820 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,387 software developers and data experts.

Help for complex query?

Hello,

I make site for sport statistic with Dreamweaver. I have problem only with head-to-head section and need ideas and help.
I have two InnoDB tables:
1. player - info for every player (player_id, player, dob, country....) and
2. matches - results from matches between players (matches_id, player1, score, player2, score2, season.......)
I made two dynamic List Menues and Go buton linked in detailed page with matches between selected player. First I tried to pass with URL parameter and with Form Variable, but no result. Now I'm thinking to make relations between two tables, but it is so complicated. In table 'players' I have name of every player in table 'matches' I have two columns with names of players? I need ideas and help how to make this complex for me query, please?
Feb 6 '10 #1

✓ answered by Atli

Hey.

We need to see the code to be able to help. (Not all of it, but the relevant parts.)

In general, you just need a <form> with two <select> boxes - one for each player - populated by fetching the player details from MySQL. Then, once two players are selected from the <select> boxes, the IDs of those players are sent to the action page, which reads them and fetches the record from the database.

6 1648
Atli
5,058 Expert 4TB
Hey.

We need to see the code to be able to help. (Not all of it, but the relevant parts.)

In general, you just need a <form> with two <select> boxes - one for each player - populated by fetching the player details from MySQL. Then, once two players are selected from the <select> boxes, the IDs of those players are sent to the action page, which reads them and fetches the record from the database.
Feb 6 '10 #2
That is good idea. Thank you!
Is that mean, that I must have two foreign keys in table 'matches' with players_id from table 'players'? Or I can use players names for foreign keys?
Feb 6 '10 #3
Atli
5,058 Expert 4TB
In a situation like that the best method would be to use a many-to-many (N:M) relationship.

One of the basic rules of database design is to never put more than one column into a table to hold the same type of value. - For example, in your table you would have to put two columns for players. Those two columns would be identical, except for the name - Now imagine if, in the future, you needed to have more than two players for a match... what would you do then? You would have to modify the table to add more player columns.

That is what a N:M relationship is meant to prevent. You basically pull the player columns out of the match table and create a intermediary table that holds a reference to a single player and a single match. That way you can add as many players as you need to any one match.

This is what a typical N:M relationship would look like:
Expand|Select|Wrap|Line Numbers
  1. +---------+     +--------------------+     +--------------+
  2. | players |     | player_match       |     | matches      |
  3. +---------+     +--------------------+     +--------------+
  4. | id (PK) |>--->| player_id (PK, FK) |    <| id (PK)      |
  5. | name    |     | match_id (PK, FK)  |<-/  | description  |
  6. | etc..   |     | player_score       |     | or_something |
  7. +---------+     +--------------------+     +--------------+
To use this effectively, you would need to use joins. (Lots of tutorials on that available via Google.) I won't go into to much detail on that, but here is an example of what those look like. This query would fetch all the players for a given match from the above tables:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     m.id,
  3.     m.description,
  4.     p.name AS 'player_name',
  5.     pm.score
  6. FROM matches AS m
  7. LEFT JOIN player_match AS pm
  8.     ON pm.match_id = m.id
  9. LEFT JOIN players AS p
  10.     ON p.id = pm.player_id
  11. WHERE m.id = 1;
This basically "joins" the tables so that we can query them as a single table. Makes it possible to filter and return data from multiple tables in a single query.
Feb 6 '10 #4
Perfect! I will try that! Thank you very much!
Feb 6 '10 #5
Hi,

Finaly I make query! Your help was great. Now I have other problem. I'm trying to pass 2 parameters from 2 drob down menus (player names) and don't know how to connect thouse 2 parameters with query in detailed page? I have two variables: $pl1 and $pl2 from drob down menus from master page. In detailed page I have folowing SELECT:

Expand|Select|Wrap|Line Numbers
  1. SELECT m.name,m.name2,score,score2
  2. FROM matches_db m JOIN players_db a ON m.name=a.name JOIN players_db b ON m.name2=b.name
  3. WHERE m.name IN ($pr1,$pr2) AND m.name2 IN ($pr1,$pr2)
may be somwhere I must have POST or GET to get thouse two variables from master page? Help, please!
Feb 25 '10 #6
Atli
5,058 Expert 4TB
Hey.

So the problem is getting the variables from the <select> boxes into the query on the detail page?

This is how something like that usually goes:
Expand|Select|Wrap|Line Numbers
  1. <!-- In master page -->
  2. <form action="details.php" method="post">
  3.     <select name="player1">
  4.         <option value="1">First</option>
  5.         <option value="2">Second</option>
  6.     </select>
  7.     <select name="player2">
  8.         <option value="1">First</option>
  9.         <option value="2">Second</option>
  10.     </select>
  11. </form>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // In details page.
  3. if(isset($_POST['player1'], $_POST['player2']))
  4. {
  5.     $player1 = mysql_real_escape_string($_POST['player1']);
  6.     $player2 = mysql_real_escape_string($_POST['player2']);
  7.  
  8.     $sql = "SELECT  stuff 
  9.             FROM    table
  10.             WHERE   player1='{$player1}'
  11.             AND     player2='{$player2}'";
  12.  
  13.     $result = mysql_query($sql) or trigger_error(mysql_error(), E_USER_ERROR);
  14.  
  15.     // Then display the results.
  16. }
  17. ?>
Always remember the mysql_real_escape_string function when using input data in a MySQL query! Arguably one of the - if not "the" - most important thing we learn about PHP. (See SQL Injection for details on that.)
Feb 26 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
1
by: Eva | last post by:
Hi, Im new to Vb.net and am trying to create my first practice project. The problem iv encountered is to do with SQL queries issued against my Access DB iv created. i want to wright a query...
14
by:  | last post by:
having a spot of trouble writing this one. if you are so inclined and have a moment, i'd really appreciate your insight. i have what amounts to a purchase order type of setup...a descriptive...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily...
1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
3
by: dd_bdlm | last post by:
Please help this one is driving me mad! I have searched and read all the topics on the error message I am receiving but none seem to apply to me! I have quite a complex query linking all parts...
8
by: babyangel43 | last post by:
Hello, I have a query set up in Access. I run it monthly, changing "date of test". I would like this query to be merged with a Word document so that the cover letter is created in Word, the fields...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.