Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two values with one query

Status
Not open for further replies.

ilmaggy

Programmer
Jan 4, 2005
11
NL
Hi all,

I want to get two values with one query:
the country_name that belongs to country_id_1 and,
the country_name that belongs to country_id_2.

I used this query:
(SELECT name FROM countries WHERE country_id = '1')
UNION
(SELECT name FROM countries WHERE country_id = '2')

But it didn't work with mysql_fetch_row / mysql_fetch_array.
Does anyone know how to do this?
Thanking you in advance,
Ruben.
 
Oh, I certainly wouldn't use a union for something that simple.

Something like:

SELECT name FROM countries WHERE country_id IN (1, 2)

or

SELECT name FROM countries WHERE coutnry_id = 1 OR country_id = 2

will do it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I agree ...

...but i think the query should work

Code:
$sql = "SELECT `name` FROM countries WHERE country_id = '1'
       UNION 
       SELECT `name` FROM countries WHERE country_id = '2'";
$result= mysql_query($sql) or die (mysql_error());
while ($row=mysql_fetch_assoc($result)):
  echo $row['name']. "<br/>";
endwhile;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top