drayarms Posted July 28, 2011 Share Posted July 28, 2011 Hello all, I'm trying to use a union query to fetch rows from unrelated tables, and display all the rows, sorting by some common critarion (in this case, the time the entry was posted/registered/uploaded). The tables are named publications and images. So here is the select statement and associated code: <?php //Connect to database require("config.php"); //Query the database. $query = "SELECT publication AS a, cartegory AS b, pub_time AS c FROM publications UNION ALL SELECT image AS a, image_cartegory AS b, image_time AS c FROM images ORDER BY c DESC"; $result = mysql_query($query); if(!$result) { die('<p>Could not retrieve the data because: <b>' . mysql_error(). '</p>'); // Handle as desired }else{ //If query is valid. if(mysql_num_rows($result) > 0){ while ($row = mysql_fetch_assoc($result)){ echo "<p>".$row['a']."</p>"; }//End of while loop }else{//If no rows were returned echo "no rows returned"; } }//Closes if query is valid statement. ?> Well as expected, all the contents from the image and publication columns are displayed in the order specified. But now my problem is, I want to be able to distinguish between the rows, depending on their table of origin, in order to format them differently. For example, let's say I want the rows from the image table to be printed out with one font style and the rows from the publications table to be printed with a different font style, how do I go about that? So far I have tried things like: //For the sake of simplicity, I won't include the html formatting here while ($row = mysql_fetch_assoc($result)){ if ($row['a'] == $row['publication']){echo $row['publication'];} else{echo $row['image'];} //OR if ('a' == 'publication'){echo $row['a'];} elseif ('a' == 'image'){echo $row['a'];} Well I hope you get the idea what I'm trying to achieve here. So far, these and similar lines I've tried only yield a blank page. I'm starting to wonder if it is even possible to format the rows differently based on their table of origin. So folks, please tell me if there is anyway what I'm trying to do can be done. Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/ Share on other sites More sharing options...
phpSensei Posted July 28, 2011 Share Posted July 28, 2011 You don need to assign new names for the columns.. use UNION without it.. Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248394 Share on other sites More sharing options...
AdRock Posted July 28, 2011 Share Posted July 28, 2011 Why don't you have an extra field in each table with their table name as the data so in the publications tanle have a field called "tname" or whatever and whenever an insert is made, insert "publications" into the column? Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248438 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 Why don't you have an extra field in each table with their table name as the data so in the publications tanle have a field called "tname" or whatever and whenever an insert is made, insert "publications" into the column? Erm...why? The question is about formating the output, it's nothing to do with the SQL or column names, or even table names - although, on the subject of table names, why didn't you just name your tables a, b, c and your fields a,b,c,d it would have saved you having to type the full names at all? Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248447 Share on other sites More sharing options...
AdRock Posted July 28, 2011 Share Posted July 28, 2011 Erm...why? The question is about formating the output, it's nothing to do with the SQL or column names, or even table names - although, on the subject of table names, why didn't you just name your tables a, b, c and your fields a,b,c,d it would have saved you having to type the full names at all? I know.... If he wants to format based on the table name, having the table name in the query will help him distingiush whcih table it's from Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248461 Share on other sites More sharing options...
AbraCadaver Posted July 28, 2011 Share Posted July 28, 2011 SELECT publication AS a, cartegory AS b, pub_time AS c, 'publication' AS `type` FROM publications UNION ALL SELECT image AS a, image_cartegory AS b, image_time AS c, 'image' AS `type` FROM images ORDER BY c DESC Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248596 Share on other sites More sharing options...
drayarms Posted July 28, 2011 Author Share Posted July 28, 2011 Thanks for all the suggestions. @ AbraCadave Did you mean 'publications' as type and 'images' as type ?? Notice that my table names are publications and images, not publication and image. those are columns that correspond to the respective tables. Thanks for the suggestion anyways. Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248719 Share on other sites More sharing options...
AbraCadaver Posted July 28, 2011 Share Posted July 28, 2011 Thanks for all the suggestions. @ AbraCadave Did you mean 'publications' as type and 'images' as type ?? Notice that my table names are publications and images, not publication and image. those are columns that correspond to the respective tables. Thanks for the suggestion anyways. It doesn't matter what you call them. It is just the value that will be returned for the pseudo column `type' You can use p and i or 0 and 1 if you want. It is just what $row['type'] will contain and corresponds to what table it came from. Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248727 Share on other sites More sharing options...
drayarms Posted July 29, 2011 Author Share Posted July 29, 2011 @AbraCadave, I noticed that b4 reading your reply. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248898 Share on other sites More sharing options...
phpSensei Posted July 29, 2011 Share Posted July 29, 2011 im just going to say this one more time OP, you dont need to define new names for your ROWS!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/243077-distinguishing-between-rows-from-a-union-query/#findComment-1248905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.