Jump to content

Retrieve table name of MySQL result


NEONecd999

Recommended Posts

show tables is the query that will return all the table in the db

Sure, but that's not what I'm looking for... I have a query that is like

SELECT * FROM table1, table2

If I pull a result from this query,

mysql_result($result,$i,"fieldname")

i'd like to know from which table, table1 or table2, did that result come from.

Link to comment
Share on other sites

show tables is the query that will return all the table in the db

Sure, but that's not what I'm looking for... I have a query that is like

SELECT * FROM table1, table2

If I pull a result from this query,

mysql_result($result,$i,"fieldname")

i'd like to know from which table, table1 or table2, did that result come from.

actuallly this is wrong

SELECT * FROM table1, table2

 

it should be the work of joining clause now on your question i say both but is not retrieving the data properly thats you also dont know where but doing that knid of query wont give a syntax errror but output error lol

Link to comment
Share on other sites

okay, i'll be more explicit..

 

$result = mysql_query("SELECT * FROM tableA UNION SELECT * FROM tableB ORDER BY RAND()");
echo mysql_result($result,0,"field"); //from which table was this result

since both tables were used in this query, and the results were randomly ordered, the first result from the query could be from either table. I need something that will return the name of the original table of this result.

 

Thanks.

Link to comment
Share on other sites

Use php to randomly select the table, then use mysql to randomly select a row...I'm assuming that's what you want...

 

$tables = array('table1', 'table2');
$which = array_rand($tables);

$result = mysql_query("SELECT fieldname FROM " . $tables[$which] . " ORDER BY RAND() LIMIIT 1") or die(mysql_error());

echo 'Result from table ' . $tables[$which] . ' was: ' . mysql_result($result, 0);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.