473,387 Members | 1,606 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.

MySQL noob question: player table, location table, JOIN

I have a player table, which is made up of fields:

id SERIAL NOTNULL AUTO_INCREMENT (this is the primary key, auto increment so each player is assigned a unique key)

player VARCHAR (just a text player name)

player_id VARCHAR(text matching the players in-game number)



then i have a locations table, with fields:

lid SERIAL NOTNULL AUTO_INC (primary key, each location is assigned a unique key)

pid INT NOTNULL (this is supposed to match the id of the player table, so this location entry gets associated with the players id)

location VARCHAR (text, one of many player's starbase locations)

econ VARCHAR (text, base stats)

def VARCHAR (text, base stats)



Question is:
how do i create an INSERT statement that inserts the players name into the player table, then uses the id from the player table, inserts that into the pid table of my locations table? then how do i query the DB to find all locations of any player, using the pid number assigned to each location?

any help is appreciated! and by the way its a very fun game, quite addictive!
Dec 25 '06 #1
1 1701
ronverdonk
4,258 Expert 4TB
You'll have to use 2 inserts because you want to use the auto_increment value of id in the second insert and the first insert has to be completed. So you combine them into 1 query like this:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO player VALUES(0,'John', '10');
  2. INSERT INTO locations VALUES(0,LAST_INSERT_ID(),'Warwick','qq', 'rr');
You are not explicit about on what basis you want to select, but assuming you want it on player's name, the statement could be like:
Expand|Select|Wrap|Line Numbers
  1. SELECT player,location FROM player,locations 
  2.        WHERE player='John' AND player.id=locations.pid;
When you want all players and their locations:
Expand|Select|Wrap|Line Numbers
  1. SELECT player,location FROM player,locations 
  2.        WHERE player.id=locations.pid
  3.        ORDER BY player;
Ronald :cool:
Dec 25 '06 #2

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

Similar topics

4
by: Bacci | last post by:
I have two tables. The first is "Locations" which has 52,000 zip codes w/cooresponding latitudes and longitudes. The second "Suppliers" has 2,000 company names and addresses. The user enters a...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
0
by: taras.di | last post by:
I'm trying to understand why MySQL isn't using all parts of the primary key when I add in an OR statement in the query. I have a people database, and a distances database. The people database...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
0
by: yeahuh | last post by:
Quick and dirty version. Godaddy server using MySQL 4.0.24 I’m trying a left join to obtain id’s in table A(cars) that are NOT in car_id in table B(newspaper): *This is a cut down version...
3
by: Choronzon | last post by:
what kind of PHP operation am i looking for when i am trying to do something like this: I have a player table, and a location table. Each player is unique, each location is unique, therefore...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.