简体   繁体   中英

get table name from union table mysql

I have union table in mysql, but I need to get name table to show my data. here mysql code

$blog = mysql_query("SELECT gambar FROM life
UNION ALL
SELECT gambar FROM places 
UNION ALL
SELECT gambar FROM style
UNION ALL
SELECT gambar FROM art
order by RAND() ");

then in php they show but I have problem for the image, as the image in different folder based on table name here my php code

 $tes = mysql_query("SELECT gambar FROM life
UNION ALL
SELECT gambar FROM places 
UNION ALL
SELECT gambar FROM style
UNION ALL
SELECT gambar FROM art
order by RAND() ");
while($b=mysql_fetch_array($tes)){
echo "<img src='img/table_name/$b[pic]'>";
}

"table_name" it should be the table name of data on union table

I think you can just add it to the subqueries:

SELECT judul, 'life' as table_name FROM life
UNION ALL
SELECT judul, 'places' FROM places 
UNION ALL
SELECT judul, 'style' FROM style
UNION ALL
SELECT judul, 'art' FROM art
order by RAND() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM