Unix Technical Forum

"Label" results of a UNION

This is a discussion on "Label" results of a UNION within the MySQL forums, part of the Database Server Software category; --> Not quite sure how to ask this... I need to do a UNION but I want to know which ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2008, 07:08 PM
petethebloke@googlemail.com
 
Posts: n/a
Default "Label" results of a UNION

Not quite sure how to ask this...

I need to do a UNION but I want to know which of the subqueries my
results came from e.g.

SELECT email, firstname, lastname FROM tbl_customer , tbl_purchase
WHERE ....

UNION

SELECT email, firstname, lastname FROM tbl_customer ,
tbl_something_else WHERE ....

But I'd like a 4th field showing the source e.g.

record_1
email = xxxxxxxx
firstname = xxxxxxxx
lastname = xxxxxxxx
came_from = purchases

record_2
email = xxxxxxxx
firstname = xxxxxxxx
lastname = xxxxxxxx
came_from = something else

Ideally, I want to get distinct customers too so that we don't spam
them
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2008, 07:08 PM
Captain Paralytic
 
Posts: n/a
Default Re: "Label" results of a UNION

On 21 Apr, 15:16, "petethebl...@googlemail.com"
<petethebl...@googlemail.com> wrote:
> Not quite sure how to ask this...
>
> I need to do a UNION but I want to know which of the subqueries my
> results came from e.g.
>
> SELECT email, firstname, lastname FROM tbl_customer , tbl_purchase
> WHERE ....
>
> UNION
>
> SELECT email, firstname, lastname FROM tbl_customer ,
> tbl_something_else WHERE ....
>
> But I'd like a 4th field showing the source e.g.
>
> record_1
> email = xxxxxxxx
> firstname = xxxxxxxx
> lastname = xxxxxxxx
> came_from = purchases
>
> record_2
> email = xxxxxxxx
> firstname = xxxxxxxx
> lastname = xxxxxxxx
> came_from = something else
>
> Ideally, I want to get distinct customers too so that we don't spam
> them


SELECT 'record_1', email, firstname, lastname FROM tbl_customer ,
tbl_purchase
WHERE ....

UNION

SELECT 'record_2', email, firstname, lastname FROM tbl_customer ,
tbl_something_else WHERE ....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 07:08 PM
petethebloke@googlemail.com
 
Posts: n/a
Default Re: "Label" results of a UNION

On 21 Apr, 16:04, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 21 Apr, 15:16, "petethebl...@googlemail.com"
>
>
>
> <petethebl...@googlemail.com> wrote:
> > Not quite sure how to ask this...

>
> > I need to do a UNION but I want to know which of the subqueries my
> > results came from e.g.

>
> > SELECT email, firstname, lastname FROM tbl_customer , tbl_purchase
> > WHERE ....

>
> > UNION

>
> > SELECT email, firstname, lastname FROM tbl_customer ,
> > tbl_something_else WHERE ....

>
> > But I'd like a 4th field showing the source e.g.

>
> > record_1
> > email = xxxxxxxx
> > firstname = xxxxxxxx
> > lastname = xxxxxxxx
> > came_from = purchases

>
> > record_2
> > email = xxxxxxxx
> > firstname = xxxxxxxx
> > lastname = xxxxxxxx
> > came_from = something else

>
> > Ideally, I want to get distinct customers too so that we don't spam
> > them

>
> SELECT 'record_1', email, firstname, lastname FROM tbl_customer ,
> tbl_purchase
> WHERE ....
>
> UNION
>
> SELECT 'record_2', email, firstname, lastname FROM tbl_customer ,
> tbl_something_else WHERE ....


Genius! Thanks. I was trying SELECT record=1, email,
firstname ....... like in PHP functions.

Thanks again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:15 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com