Author Topic: Selecting from multiple tables  (Read 897 times)

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Selecting from multiple tables
« on: January 13, 2011, 09:36:20 AM »
I have been working on my game and I have code like this:

Code: [Select]
$grabItem = mysql_query("SELECT * FROM equipment_inventory AS inv, equipment_templates AS temp WHERE inv.item_id = temp.item_id");
$item = mysql_fetch_array($grabItem);

Now, assume I want to grab the value item_id from the inventory and the templates table using this code

Code: [Select]
echo $item['user_id'];
Well, as you can see, there are 2 user_id values. One from inventory and one from templates. How would I force it to grab from one or the other table? I tried
Code: [Select]
echo $item['inv.user_id']; and I had no luck...

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Selecting from multiple tables
« Reply #1 on: January 13, 2011, 09:59:18 AM »
Afaik in case of duplicit names you must alias them (equipment_templates.id as temp_user_id)

what's the purpose of user_id in equipment template?
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Selecting from multiple tables
« Reply #2 on: January 13, 2011, 10:58:04 AM »
You really should redesign your database schema, all "user_id" should point to the same data, so it does not matter from which table you read it.

BTW,  equipment_templates shoud never hold user_id or any other user indication, since it is a global description table not connected to user.

If you don't want to then aliases as Nox described.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal