npsari Posted May 12, 2007 Share Posted May 12, 2007 Hi there, I have table called Info. There are two main colombs (Name, News) I try display each distinct Name and his latest News: $q = "SELECT DISTINCT Name FROM info ORDER BY Date ASC, Time ASC LIMIT 10;"; $res = @mysql_query($q); while($r = @mysql_fetch_array($res)) { print "$r[Name]"; print "$r[News]"; } But the "News" dont show in Results! I want the latest News of a Distinct Name to show too What should i add to mySQL code guys Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/ Share on other sites More sharing options...
taith Posted May 12, 2007 Share Posted May 12, 2007 a) distinct also should use group by b) you need to "distinct(name), news" Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251242 Share on other sites More sharing options...
npsari Posted May 12, 2007 Author Share Posted May 12, 2007 I tried it: $q = "SELECT DISTINCT(Name), News FROM info ORDER BY Date ASC, Time ASC LIMIT 10;"; however, it Doesnt work! It shows the names repeated too Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251243 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 Why can't you just do this: <?php $conn = mysql_connect("localhost", "user", "pass") or die(mysql_error()); $q = mysql_query("SELECT * FROM info ORDER BY Date ASC, Time ASC LIMIT 10;") or die(mysql_error()); while($r = mysql_fetch_array($q)) { echo $r['Name']."\n\n"; echo $r['News']."\n\n"; } ?> I always find it easier to select * and just fish out what you need. Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251245 Share on other sites More sharing options...
taith Posted May 12, 2007 Share Posted May 12, 2007 a) distinct also should use group by Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251247 Share on other sites More sharing options...
npsari Posted May 12, 2007 Author Share Posted May 12, 2007 Oh, sorry, I used the GROUP BY now $q = "SELECT DISTINCT(Name), News FROM info GROUP BY Name ORDER BY Date DESC, Time DESC LIMIT 4;"; It arranges all Names DESC But it doesnt show the latest News of the person DESC It shows his First News Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251250 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 What is "Message" ~ you haven't mentioned that before? Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251251 Share on other sites More sharing options...
npsari Posted May 12, 2007 Author Share Posted May 12, 2007 Sorry, Message is News And Chningly, i tried your code It shows everything!!! But thanks pal Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251254 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 @chigley I always find it easier to select * and just fish out what you need. For easier, read "lazier". The query is less efficient, retrieving unnecessary data. And from a maintenance viewpoint when you or someone else reads it later, you cannot easily tell what the query is really for. @npsari SELECT n.name, n.news FROM news n INNER JOIN (SELECT id, MAX(CONCAT(date,time))as latest) as a ON n.id = a.id AND CONCAT(n.date,n.time) = a.latest Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251261 Share on other sites More sharing options...
npsari Posted May 12, 2007 Author Share Posted May 12, 2007 I tried to explain my needs as much as I could Anyway, i tried this code, it didnt show nothing this time Its ok, i will try on it myself more Thanks Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251262 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 Not necessarily lazy as I often then use other values from the table later in the code. I can see why it is interpreted as lazy though! Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251269 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 Sorry, my wife was nagging to go shopping and I forgot the FROM and GROUP BY in my haste SELECT n.name, n.news FROM news n INNER JOIN (SELECT name, MAX(CONCAT(date,time)) as latest FROM news GROUP BY name) as a ON n.name = a.name AND CONCAT(n.date,n.time) = a.latest Quote Link to comment https://forums.phpfreaks.com/topic/51054-a-little-mysql-hustle-here-cant-figure-something-out/#findComment-251282 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.