/    Sign up×
Community /Pin to ProfileBookmark

Differentiating SQL UNION Sub result sets

Hi,

I am in need of doing some conditional formatting on following.

If we use an SQL UNION to join resultsets of two SQL queries,

once we get resultset of combined resultset,

is there is a way to detect, from which sub query this particular record belongs to.

Say we use,

SELECT * FROM table1 UNION SELECT * FROM table2

Now we get combined resultset, and I want to display records (that part no problem ? ), and make text color red of records belonging to second sub query, that is of table2.

Can it be possible ? Do we at least have an indirect way ?

Thanks and Best Regards

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 06.2008 — you could do

<i>
</i>select
'table1', *
from table1
union
select
'table2', *
from table2
Copy linkTweet thisAlerts:
@GUIRauthorDec 06.2008 — Hi,

I didnt understand your post as I am not much experienced on this area.

Anyway I have a SQL UNION based functioning query, what I want is to while printing results with PHP to detect from which table this record came from, and if table 1 make highlighted appearnce
Copy linkTweet thisAlerts:
@GUIRauthorDec 06.2008 — Hi,

I was able to get use of your tip, it really worked, never thought it would be this simple.

Thanks a lot.

SELECT 'table1' as table,table1.* FROM table1

UNION

SELECT 'table2' as table,table1.*
FROM table2
×

Success!

Help @GUIR spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...