Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Mysql Problem, Linking tables
Scene
post Apr 14 2008, 04:27 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 1
Joined: 14-April 08
Member No.: 29,791



Hello guys,

I'm making a PHP/MySQL site for a friend of mine who plays an online game where you can create towns, add buildings to them and upgrade them to a newer level.

I've offered to make him a small site where he can input his data into a database and keep track of his towns, buildings and levels.

I'll show you my database design and then explain what i want to do.

Table: towns

field 1: TownID (primary key, auto-increment)
field 2: TownName
field 3: TownType
field 4: Town Description

Table: buildings

field 1: BuildingID (primary key, auto-increment)
field 2: BuildingName
field 3: BuildingType
field 4: BuildingDescription

Table: evolution

field 1: EvolutionID (primary-key, auto-increment)
field 2: TownID (foreign key,) / link to TownID in towns table
field 3: BuildingID (foreign key) / link to BuildingID in buildings table
field 4: Level / current building level

In essence i want to use the evolution table to combine the previous tables, and generate an overview showing the buildings each town has and on what level these buildings are.

My current SQL query is:

CODE
$sqlOverview = mysql_query("
SELECT *
FROM buildings b
JOIN evolution e ON b.BuildingID = e.BuildingID
JOIN towns t ON e.TownID = t.TownID");


Which gives me the following output:



As you can see it prints every building in a separate HTML table,
what i'd like is that each building that is linked to a town to be shown in the same table.

My guess is my SQL query is causing this but i can't figure out how i can resolve this..

Any help would be appreciated,

Kind regards,
Scene

PS. english is not my first language, if i dont make sense or additional explaining is required please let me know.
Go to the top of the page
 
+Quote Post
faulty.lee
post Apr 14 2008, 06:24 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 442
Joined: 5-November 06
Member No.: 17,016



QUOTE(Scene @ Apr 15 2008, 12:27 AM) *
CODE
$sqlOverview = mysql_query("
SELECT *
FROM buildings b
JOIN evolution e ON b.BuildingID = e.BuildingID
JOIN towns t ON e.TownID = t.TownID");


All you need to do is use "GROUP BY"
CODE
$sqlOverview = mysql_query("
SELECT *
FROM buildings b
JOIN evolution e ON b.BuildingID = e.BuildingID
JOIN towns t ON e.TownID = t.TownID")
GROUP BY t.TownID;


I do have a bit of suggestion on your table structure. Since all your evolution are tied one-to-one to your building, you might as well just store the level at the building's table.

Table: buildings

field 1: BuildingID (primary key, auto-increment)
field 2: BuildingName
field 3: BuildingType
field 4: BuildingDescription
* Additional
field 5: TownID (foreign key,) / link to TownID in towns table
field 6: Level / current building level


Unless of cause you want to store every evolution that building has been through, then you need to fetch the latest evolution from your table. When you start using group by, not all the data that falls under the same group is shown. Normally group by are used for counting subtotal of each groups.

NOTE : I just notice that you want to show multiple tables of town, with buildings in each town's table. That you'll need to run your query for towns, and generate each table from there, while looping through, do another query to fetch buildings for that particular town, and display into the table.

-> Loop through each town
-> Loop through each building of that town

JOIN is to combine and tabulate everything in one table
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL Realtime Replication(3)
  2. Error In Installing MySQL Server(6)
  3. Recover Tables From A MySQL .frm File(6)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. How To Connect MySQL With Flash?(8)
  6. MySQL, Multiple Tables(22)
  7. Linking Two Tables(12)
  8. Access -> Mysql(8)
  9. Is It A Good Practice To Store Image Or Other Binary Files Directly In A Mysql Database(4)
  10. Navcat For MySQL(7)
  11. Permission Problem With Mysql Database Creation(8)
  12. Mysql And Php(15)
  13. Problems With Php Saving Data Into Mysql(6)
  14. Login System Using A Mysql Db(4)
  15. Mysql Question...(6)
  1. Oracle Vs. Mysql Vs. Postgresql(9)
  2. Script Tables On Sql Server Compact(0)
  3. Installing Mysql?(17)
  4. Mysql Query Question(3)
  5. Lost Connection To Mysql ,..., System Error: 111 In(3)
  6. Subqueries In Mysql(1)
  7. Apache Php With Mysql On Windows [solved](9)
  8. Not Understanding Mysql(4)
  9. Mysql Script Help(3)
  10. Mysql - So Hard(14)
  11. Sun Bought Mysql(6)
  12. Mysql Backup With Another Address?(4)
  13. I Have An Error With My Mysql Connection(7)


 



- Lo-Fi Version Time is now: 6th July 2008 - 09:56 AM