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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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 .... |
| ||||
| 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. |