xoshbin Posted April 25, 2011 Share Posted April 25, 2011 hi im new here and i hope being helpfull and help me too.i have this code i want to select latest records from three tables (order by id) it works fine but its adds records of latest table on the bottom and the second one betwen first table and third table. what i want is when i add a record for the third one goes to top not in the botom here's the code: $result = mysql_query('(SELECT colum1, colum2 FROM tbl1 ORDER BY id DESC LIMIT 2) UNION ALL(SELECT colum1, colum2 FROM tbl2 ORDER BY id DESC LIMIT 1) UNION ALL(SELECT colum1, colum2 FROM tbl3 ORDER BY id DESC LIMIT 1)');while($row = mysql_fetch_array($result)){} Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 You should be able to use another ORDER BY at the end for the entire result. It's not necessary to order results from each table, you can just order all of them at the end after the union. Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Thanks for your repliehere its what i did if you mean that but still not working $result = mysql_query('(SELECT colum1, colum2 FROM tbl1) UNION ALL(SELECT colum1, colum2 FROM tbl2) UNION ALL(SELECT colum1, colum2 FROM tbl3) ORDER BY id DESC LIMIT 4'); Thanks for your time Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Anyone else? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 You may need to add the id to the list of columns to select if you're going to be ordering by that. I'm not sure how you want it to work, but you may also want to put the LIMIT statements back with each table (as it is now, it gets the 4 most recent regardless of which table they come from). Other than that, if it's not working let me know what it is or is not doing that's wrong. Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 All of the tables has id columns. i want to select 4 records from these tables order by latest one added, and the tables has date column i have tried to order lts records by date but still not works here is how for the date $result = mysql_query('(SELECT colum1, colum2 FROM tbl1 ORDER BY date DESC LIMIT 2) UNION ALL(SELECT colum1, colum2 FROM tbl2 ORDER BY date DESC LIMIT 1) UNION ALL(SELECT colum1, colum2 FROM tbl3 ORDER BY date DESC LIMIT 1)'); Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 I'm saying try something like this:$result = mysql_query('(SELECT id, colum1, colum2 FROM tbl1) UNION ALL(SELECT id, colum1, colum2 FROM tbl2) UNION ALL(SELECT id, colum1, colum2 FROM tbl3) ORDER BY id DESC LIMIT 4');Notice the IDs are in the select list. If that doesn't work, tell me what "doesn't work" means. Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Ok thanks thats great worked But i tried this to select latest record by date but didnt work $result = mysql_query('(SELECT date, colum1, colum2 FROM tbl1) UNION ALL(SELECT date, colum1, colum2 FROM tbl2) UNION ALL(SELECT date, colum1, colum2 FROM tbl3) ORDER BY date DESC LIMIT 4'); Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 What's different between that and the one that works? Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Actually nothing, i think the problem is with my database.Thanks justsomeguy you are great you really helped me. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 PHP won't automatically print an error from MySQL, you have to check for that yourself:$result = mysql_query('...') or exit(mysql_error()); Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Actually it works but it returns first records not latest records Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2011 Share Posted April 25, 2011 What is the data type of the date field? Link to comment Share on other sites More sharing options...
xoshbin Posted April 25, 2011 Author Share Posted April 25, 2011 Date Link to comment Share on other sites More sharing options...
justsomeguy Posted April 26, 2011 Share Posted April 26, 2011 It should be sorting dates correctly as long as the dates in the table are correct. If that field is a varchar field it will sort it alphabetically. Link to comment Share on other sites More sharing options...
xoshbin Posted April 26, 2011 Author Share Posted April 26, 2011 Very big thanks for your time, I have solved the problem it was from my database i didnt add the records on date fields. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.