Jump to content


Photo

Multiple tables


  • Please log in to reply
4 replies to this topic

#1 TEENFRONT

TEENFRONT
  • Members
  • PipPipPip
  • Advanced Member
  • 338 posts

Posted 04 October 2006 - 04:46 AM

Hey Hey

I have a database with 3 tables. Tables are, users1, users2, and users3. to make things simple lets say they all have 1 column "username".

I wanna do this
$username = "Adam";
$sql ="SELECT username FROM user1, user2, user3 WHERE username = '$username'";


But the username, Adam, is only in 1 of the 3 tables. So the above doesnt work, it just fails to find Adam. Yet if i just do it with 1 table like this..

$username = "Adam";
$sql ="SELECT username FROM user1 WHERE username = '$username'";

it will find Adam if Adam is in users1.

I need to be able to locate Adam from all 3 tables. How do i do this?
Teenfront.co.uk : Free Teen Chat -  FunkySmileys.co.uk : MSN Smileys -  ArcadeMonkey.co.uk : Free Flash Games
8Baller.co.uk : Free Multiplayer Pool [url=http://8Baller.co.uk]

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 04 October 2006 - 04:49 AM

You could do 3 different queries... or a join query... but im half asleep so i cant think of how to do that :P
Why doesn't anyone ever say hi, hey, or whad up world?

#3 TEENFRONT

TEENFRONT
  • Members
  • PipPipPip
  • Advanced Member
  • 338 posts

Posted 04 October 2006 - 04:52 AM

i was kinda hoping i wouldnt have to do 3 queries..

username is unique in all 3 tables.. so surley it can select username from 3 tables and return 1 result?
Teenfront.co.uk : Free Teen Chat -  FunkySmileys.co.uk : MSN Smileys -  ArcadeMonkey.co.uk : Free Flash Games
8Baller.co.uk : Free Multiplayer Pool [url=http://8Baller.co.uk]

#4 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 04 October 2006 - 11:37 AM

well i thought it was this.....

$username = "Adam";

$sql = "SELECT user1.username AS user1, user2.username AS user2, user3.username AS user3 FROM user1, user2, user3 WHERE user1.username = '$username' OR user2.username = '$username' OR user3.username = '$username';";
$results = mysql_query($sql) or die(mysql_error());

while($array = mysql_fetch_array($results))
{

echo $array['user1'];

echo"<br><br>";

echo $array['user2'];

echo"<br><br>";

echo $array['user3']; 
}

but it only works if Adam is in all 3 tables.... so maybe someone could help

#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 04 October 2006 - 01:46 PM


(SELECT username FROM user1 WHERE username = '$username')
UNION (SELECT username FROM user2 WHERE username = '$username')
UNION (SELECT username FROM user3 WHERE username = '$username')






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users