Jump to content

Recommended Posts

How do I write this SQL statement such that i could get that output?

 

"Getting the category of the latest updated news."

 

==SQL Table==

ID CATEGORY

76 Sports

75 Sports

74 Entertainment

73 Sports

72 Business

71 World

70 Business

 

==OUTPUT==

Sports

Entertainment

Business

World

 

There are still some more other data in the SQL Table such as news title, date etc.

 

I just need the sql statement for "Getting the category of the latest updated news."

 

Mine was "Select distinct last(cat) order by ID desc" but it couldn't work.

 

I added the LAST() was because if not it will look like this

 

 

==SQL Table==

76 Sports

.

.

.

6 Sports

5 Entertainment

4 Sports

3 Business

2 World

1 Business

 

==OUTPUT==

Entertainment

Sports

World

Business

 

Any kind soul to help me?? thanks!!

Link to comment
https://forums.phpfreaks.com/topic/162208-solved-simple-sql-statement-help/
Share on other sites

Nope, aint working

 

this is my sql table

 

IDSportsName

10SoccerJohn

9BasketballAlan

8SoccerDean

7BaseballCalvin

6RugbyTosh

5GolfTobey

4BaseballPeter

3SoccerMicheal

2GolfPatrick

1BaseballLuke

 

I wanna get the 5 latest Sports that the new members have chosen which should be...

 

1. Soccer

2. BasketBall

3. Baseball

4. Rugby

5. Golf

 


Select CATEGORY

FROM newtable

order by ID desc

LIMIT 0,1

 

Will get...

 

1. Soccer

2. Basketball

3. Soccer

4. Baseball

5. Rugby

6. Golf

7. Baseball

8. Soccer

9. Golf

10. Baseball

You should really TIMESTAMP your entries.  There's no reason not to...

 

Assuming the greatest 5 ID's would give you the latest 5 entries:

 

Select Sports FROM table GROUP BY Sports ORDER BY ID DESC LIMIT 5

 

That would result in duplicate output

1. Soccer

2. Basketball

3. Soccer

4. Baseball

5. Rugby

Hi

 

I wanna get the 5 latest Sports that the new members have chosen which should be...

 

Right, so you don't want duplicates.

 

As such this should give you the latest 5 :-

 

SELECT Sports, Max(id) As MaxId 
FROM `SportsTable` 
GROUP BY Sports 
ORDER BY MaxId DESC 
LIMIT 0,5

 

All the best

 

Keith

I see, but doesn't the ORDER BY and GROUP BY take care of that?  My logic is that if you're ordering by 'ID DESC', which orders the list greatest to least by ID, and 'GROUP BY Sport', which would rid duplicates, then you wouldn't need to use MAX() because these 2 clauses assume that.

Hi

 

My understanding is that the GROUP BY will return one record per grouped value, with the other values being from an effectively random row with that value. The order by clause will be after the group by, hence will work on what is left after the group by has finished (ie, order on the randomly selected ID field).

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.