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.

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

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.

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.