Selecting multple tables or DB

C++, C#, Java, PHP, ect...
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Selecting multple tables or DB

Post by hallsofvallhalla »

didn't know if any of you use Union but if lets say you need to select every player, creature and every pet that has 100 gold and 10 hit points and players, creatures and pets are in different tables

Code: Select all

$result="SELECT name from players where hitpoints=10 AND gold =100 UNION ALL
SELECT name from creatures where hitpoints=10 AND gold =100 UNION ALL
SELECT name from pets where hitpoints=10 AND gold =100 UNION ALL
    $result2=mysql_query($result) or die("Could not get result");
     while($result3=mysql_fetch_array($result2))
	{
	
	echo $result3['name'] . "<br>";
	
	}
this will search all 3 tables and echo out all who meet the requirements in one nice query :)
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Selecting multple tables or DB

Post by ConceptDestiny »

Do those tables selected not have to have the same number of columns?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Selecting multple tables or DB

Post by hallsofvallhalla »

yes they do actually.
Post Reply

Return to “Coding”