Forum Moderators: open

Message Too Old, No Replies

Is it possible to find out what table the row in exists?

         

scraptoft

5:54 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



I am using a UNION to search through multiple tables and select a unique row by ID.

Is it possible to find out which table the row exists in?

[edited by: scraptoft at 6:08 pm (utc) on Oct. 15, 2007]

LifeinAsia

6:02 pm on Oct 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Maybe something like this:
SELECT 'TableA' AS TableName
FROM TableA
WHERE ID=UniqueID
UNION
SELECT 'TableB' AS TableName
FROM TableB
WHERE ID=UniqueID

scraptoft

6:13 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



Hi lifeinasia, I don't quite follow. I want to use TableName in an if statement later on.

If I were to use your code would this work:

if ($TableName == bluetable) {echo "You are using bluetable";

Demaestro

6:35 pm on Oct 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What LIA is showing you is that you can "hard code" the table name into the select statement as a text field and you can name that column anything... in his example he called it TableName....

so in your last example you would........

SELECT 'bluetable' AS TableName, ID, column1
FROM bluetable
WHERE ID=UniqueID
UNION
SELECT 'redtable' AS TableName, ID, column1
FROM redtable
WHERE ID=UniqueID

Notice that the table that you are selecting from is "hard coded" as a string to be returned on every side of the UNION.

These tables:

bluetable
ID ¦ column1
-------------
1 ¦ 'blue text'
2 ¦ 'blue text2'

redtable
ID ¦ column1
-------------
1 ¦ 'red text'
2 ¦ 'red text2'

Would output:

Id ¦ column1 ¦ TableName
----------------
1 ¦ 'red text' ¦ 'redtable'
1 ¦ 'blue text'¦ 'bluetable'
2 ¦ 'red text2' ¦ 'redtable'
2 ¦ 'blue text2'¦ 'bluetable'

You can then use this line....

if ($TableName == 'bluetable') {echo "You are using bluetable"};

[edited by: Demaestro at 6:43 pm (utc) on Oct. 15, 2007]

scraptoft

6:48 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



ahh I see, Thanks a lot for your time Demaestro you have clarified that very well, much appreciated. Thank you both.

Kind regards,

LifeinAsia

6:50 pm on Oct 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if ($TableName == bluetable) {echo "You are using bluetable";

You wouldn't even need to use the if statement. You have the aliased field name- just use it directly.
I don't know how to concatenate data in PHP, but I assume it would be something like:
echo "You are using " + $TableName;