Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UNION Query; Determine which table record is from

Author  Topic 

whoshotdk
Starting Member

2 Posts

Posted - 2010-07-09 : 13:04:09
Hi there

Im creating a 'profile' based website; i.e people can get their own profile page and store their details on it. Profiles can be placed in a number of 'areas'.

In addition, their profiles can be 'featured' on one or more area pages. However, they can be featured in two different ways - either as a plain old 'featured' profile, or as a 'touring' profile.

So, I have four tables. A 'profiles' table which contains all the profiles. An 'areas' table which contains all the areas. Also two 'lookup' tables: 'featuredAreaLookup' and 'touringAreaLookup'.

The lookup tables just have two columns in them: 'profileID' and 'areaID'.

profileID references the 'id' PK in the 'profiles' table.
areaID references the 'id' PK in the 'areas' table.

If a profile's ID appears in one of those lookup tables; then it is 'featured'.

Im using the following query to get all the 'featured' profiles (using area ID 2 as an example):

SELECT * FROM profiles, featuredAreaLookup WHERE areaID = 2 AND profileID = id UNION SELECT * FROM profiles, touringAreaLookup WHERE areaID = 2 AND profileID = id

This works fine; I'm using PHP to loop through the results and output a list of all the 'featured' profiles.

However, for each record I would like to determine if it came from the 'featuredAreaLookup' table or the 'touringAreaLookup' table.

I assume I could either:

A. Do two queries instead of the one UNION query
B. For each record looped, do another query (or two) to see which lookup table it appears in.

However, I think these would be inefficient?

I was thinking perhaps I could add to the above query a temporary' column that would basically say either 'featured' or 'touring' depending on the lookup table it appears in.

I just dont have a clue how to do it!

Any suggestions most welcome.
Thanks,
Dave

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-09 : 13:18:49
in each query you place a hard coded value so you can see from which select the data is coming.
SELECT 'featured' as tabname,*
FROM profiles, featuredAreaLookup WHERE areaID = 2 AND profileID = id
UNION
SELECT 'touring' as tabname,*
FROM profiles, touringAreaLookup WHERE areaID = 2 AND profileID = id


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

whoshotdk
Starting Member

2 Posts

Posted - 2010-07-09 : 13:50:49
Ingenious! Many thanks!

A quick note for posterity: the query didn't work straight away; just needed to re-jig the order of the SELECT arguments to place the '*' first:

SELECT *, 'featured' AS tabname FROM profiles, featuredAreaLookup WHERE areaID = 2 AND profileID = id UNION SELECT *, 'touring' AS tabname FROM profiles, touringAreaLookup WHERE areaID = 2 AND profileID = id ORDER BY RAND() LIMIT 0, 2

Thanks again,
Dave
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-09 : 13:53:40
welcome
but think about it: this isn't a forum for MySQL so sometimes you can't get the right solution here...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-09 : 15:23:15
quote:
Originally posted by whoshotdk

Ingenious!



Shouldn't that be "BRILLIANT!!!!"

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -