How to UNION ALL two SELECT statements?

Posted by Lisa on Stack Overflow See other posts from Stack Overflow or by Lisa
Published on 2010-05-05T01:35:08Z Indexed on 2010/05/05 1:38 UTC
Read the original article Hit count: 180

Filed under:
|

I have 2 tables, one looks like this:

TABLE ONE

id | Last Name | First Name | Username | Password | Secret Question

and another that looks like this:

TABLE TWO

id | Hobby | Country | 

I want to combine a Select statement that grabs data from both tables and output the results. The following code:

$select = mysql_query("

    SELECT * FROM table_one WHERE Username = 'Bob'

    UNION ALL

    SELECT * FROM table_two WHERE Hobby = 'Baseball'

");

while ($return = mysql_fetch_assoc($select)) {

$userName = $return['Username'];

$hobby = $return['Hobby'];

}

echo "$userName likes $hobby";

results in a The used SELECT statements have a different number of columns error, what am I doing wrong?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php