Jump to content

SQL query to export to CSV


genius_supreme

Recommended Posts

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.