genius_supreme Posted February 25, 2012 Share Posted February 25, 2012 Dear freak gurus out there, I'm new to php and sql as i learn from online examples . Though there are lots of links and answers out there related to my post but none reflects what i need. You guys my only hope. what i'm trying to accomplish: i have 3 tables (tblmember,tblspouse,tblchildren) for family details and contact record which UNION with field:FamilyName. tblmember consists of additional 5 fields for contacts details such as address,postal,state,etc. Each tables also has a field for membership status field:MembershipType. i would like to generate a "report" in csv format where the records are exported based on membership status selected from the dropdown list on the reports.php page. Once clicked on the submit button the query will do the "magic sql" to capture the selected fields from all the 3 tables based on MembershipType = $_POST['dropdownlist'] I have done a search query using UNION where the user enters anypart of the "member name" and the query will look into field:MemberName in all 3 tables and displays the selected fields from all 3 tables onto the page itself (no export). can i combine the SELECT * INTO OUTFILE with UNION? as i received an error "incorrect usage of UNION and INTO". so how do i use UNION with SELECT INTO OUTFILE? and is this the right way? i have run 2 separate test query: 1) with UNION works well on all 3 tables just to print on page in tabulated format 2) with INTO OUTFILE works well to export only with one table is there a way to have 1 query to do the both? Quote Link to comment https://forums.phpfreaks.com/topic/257731-sql-query-to-export-to-csv/ Share on other sites More sharing options...
ignace Posted February 25, 2012 Share Posted February 25, 2012 http://bugs.mysql.com/bug.php?id=34665 See the response by "[4 Mar 2008 11:24] Martin Hansson" A viable alternative is: CREATE TEMPORARY TABLE temp ENGINE=MEMORY ( SELECT foo FROM bar WHERE bat = baz ) UNION ( SELECT foo FROM bar WHERE bat = baz ) Then SELECT * FROM temp INTO OUTFILE ... DROP TABLE temp; EDIT: SELECT * FROM ( (SELECT foo FROM bar) UNION (SELECT baz FROM bat) ) as dummy INTO OUTFILE 'foo.csv' Works equally. Quote Link to comment https://forums.phpfreaks.com/topic/257731-sql-query-to-export-to-csv/#findComment-1321069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.