tags:

views:

56

answers:

2

Hi,

I have 2 tables catelog and catelog copy. And, I need to to display both these table fields as one table using mysql.

Can anyone correct this code for joining 2 tables?

$result = mysql_query("SELECT * FROM catelog WHERE title='".$fileid."'union
select status  from catelog_copy " );
A: 

You could simply use

SELECT c1.*, c2.* FROM catalog as C1, catalog_copy as C2
where c1.Title = 'blah'

if no logic combining the results is required. But that seems silly...

What i think you want to do is

( select * from catalog 
  where Title = 'title' )
union
( select * from catalog_copy   
  where Title = 'title')
Ruben Steins
Oops, this would produce one big cross product, I doubt that is what OP has in mind...
mjv
@mjv: that's true. But it's pretty unclear to me what the OP has in mind :) But I've added a second option to please everyone.
Ruben Steins
+2  A: 

SELECT statements you'd like to UNION have to return the same number of columns and types of those columns should match.

So

SELECT a, b, c FROM table1 UNION SELECT a, b, c FROM table2

will work, but

SELECT a, b FROM table1 UNION SELECT a, b, c FROM table2

will not.

Names of the fields and the fields themselves might be different though.

vava
The requirement for UNION is that the select list for each query in the UNION provides the same number of columns, and these should be of matching types. The names for the columns in the result come from the name of the column/aliases of the first query.
mjv
@mjv, fixed :) Although it makes explanation more confusing.
vava