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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting 2 mysql tables

Status
Not open for further replies.

andersgp

Technical User
Mar 13, 2004
2
NO
I have two tables, lets call them table1 and table2, and they both have the same field month, amongst other fields.

I want to select all the rows in both tables where the field month is equal to e.g. March.

I have tried various ways to to this, but it won't succeed.

I'm using MySQL 4.0.18-standard and PHP 4.3.3.

Looking forward to getting help!
 
If the result sets from each table have different structures, you would need 2 separate queries, for example:
SELECT * FROM TABLE1 WHERE MONTH=3
and
SELECT * FROM TABLE2 WHERE MONTH=3

If the result sets from each table have the same structure, you could use a UNION select, for example:
SELECT * FROM TABLE1 WHERE MONTH=3 UNION SELECT * FROM TABLE2 WHERE MONTH=3
 
If they have a different structure then you can still UNION them. You will need to create calculated fields in each select so you have matching fields and types in the two recordsets.

 
Thank you very much. I succeded.

My follow-up question will then be:
Is there any variabel which can tell me if the rows I've selected are from table1 or table2, after I've used UNION?
 
Just create another calculated field and set it to 1 in one table and 2 in the other.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top