I am trying to join the results from 2 tables and display the results. Can anyone tell what I am doing wrong here?? My Code: <? header("Content-type: application/octet-stream"); header("Content-disposition: attachment; filename=Customer_data".date("Y-m-d").".csv"); header("Pragma: no-cache"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Expires: 0"); mysql_connect("localhost", "***", "***"); mysql_select_db("***"); echo "customers_firstname,customers_lastname,customers_dob,customers_email_address,customer_company,street_address,suburb,post_code,city,state,country,zone,customers_telephone,customers_fax\n"; $query = mysql_query("SELECT * FROM customers) UNION ALL (SELECT * FROM address_book"); while($row = @mysql_fetch_array($query)) echo "$row[customers_firstname], $row[customers_lastname], $row[customers_dob], $row[customers_email_address], $row[entry_company], $row[entry_street_address], $row[entry_suburb], $row[entry_postcode], $row[entry_city], $row[entry_state], $row[entry_country_id], $row[entry_zone_id], $row[customers_telephone], $row[customers_fax]\n"; @mysql_free_result($query); ?> What I Get: customers_firstname,customers_lastname,customers_dob,customers_email_address,customer_company,street_address,suburb,post_code,city,state,country,zone,customers_telephone,customers_fax This code is exporting a CSV file from the two mySQL Tables. If i take away the join statement and one of the tables I ask it to query, it prints results, not all of the results I need though. Does anyone know what I need to change?
It sounds like the structures of the two tables are different. In order for UNION to work the two select statements *must* return the same number of fields, they must have exactly the same name, they must be in the same order and they must be of the same datatype. If the tables have a different number of fields or the fields are in a different order you can fix it by specifying the fields explicitly in the select statements instead of using "*". This will limit the fields returned and fix them in the same order. If the fields are a different datatype in the two tables then you need to convert the field in one select statement to the datatype in the other. How you do that will depend on the database engine your using. If the fields have different names in each table, then you need to rename the field in one select statement to be the same as in the other.