c_pattle Posted January 20, 2011 Share Posted January 20, 2011 I was wondering if anyone know's how to create a field in a mysql query. For example if I have a query such as "select * from names" is there a way to add a field that automatically increment for search row. So for example the query would then return a list of names each with a unique number (e.g 1-10) even though this field is not in the database. I want to do this because I want to join two query and I want to create a common field which both queries will have. Thanks for any help. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 20, 2011 Share Posted January 20, 2011 this is one way to do it SELECT @rownum:=@rownum+1 AS rownum, a.* FROM <YOUR TABLE> AS a, (SELECT @rownum:=0); but... why you need to do this? Quote Link to comment Share on other sites More sharing options...
c_pattle Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks, I need to do it because I need to join 2 tables together. I tried using a left join but because there are no common fields it means that I just got "null" for all of the values from the right table. However if I have a common field then the join will work. Hope that makes sense. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 20, 2011 Share Posted January 20, 2011 Thanks, I need to do it because I need to join 2 tables together. I tried using a left join but because there are no common fields it means that I just got "null" for all of the values from the right table. However if I have a common field then the join will work. Hope that makes sense. No really... doesn't make sense... doesn't "smell" good ... what are you trying to do exactly?... maybe the description of your 2 tables and a little explanation of your objectives will help you to get another point of view Quote Link to comment Share on other sites More sharing options...
c_pattle Posted January 20, 2011 Author Share Posted January 20, 2011 Yeah I am open to suggestions on how to make it better. Basically I have two tables that are completely unrelated. I want to have a query that will get the most recent 10 rows from both tables but will merge the two tables into one. I want to do this because it will be a lot easier when I come to actually doing something with the data. For example if I do.. while ($array = mysql_fetch_array($rs)) { It will be a lot lot easier if all the data I need is on the same row. Quote Link to comment Share on other sites More sharing options...
DarkKnight2011 Posted January 20, 2011 Share Posted January 20, 2011 hi, if you post your tables structure and some sample data im sure someone will be able to simplify your query to match what you want, it does seem like a very strange way of doing anything to be honest Quote Link to comment Share on other sites More sharing options...
c_pattle Posted January 20, 2011 Author Share Posted January 20, 2011 Here are the two tables that I have. With both tables I have included an examples of the latest row that could have been entered. The first is a table called "articles". article_numberarticle_titlearticle_descriptionarticle_authorarticle_link 45Article 1A little description hereChris Pwww.google.com The second is called "announcements". announcement_numberannouncement_titleannouncement_contentannouncement_link 10Announcement 1 The content for announcementwww.bbc.co.uk Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 20, 2011 Share Posted January 20, 2011 use UNION http://dev.mysql.com/doc/refman/5.1/en/union.html restriction: Selected columns listed in corresponding positions of each SELECT statement should have the same data type Quote Link to comment Share on other sites More sharing options...
c_pattle Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks but the problem I have with union is that it will but the records from each tables on different lines and I want it all on one line Quote Link to comment Share on other sites More sharing options...
suresh_kamrushi Posted January 21, 2011 Share Posted January 21, 2011 You can try this, SELECT articles. * , announcements. * FROM articles, announcements LIMIT 1 Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 21, 2011 Share Posted January 21, 2011 You can try this, SELECT articles. * , announcements. * FROM articles, announcements LIMIT 1 that is not what the OP want: I want to have a query that will get the most recent 10 rows from both tables but will merge the two tables into one Quote Link to comment 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.