473,505 Members | 14,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simpleish sort question?

Hi yall

I'm a newbie, forgive me...

I am sure I have seen a simple way to do this, but I can't for the life of
me remember how it was done...

Say there is a database of jokes, from different jokers...

Some jokers submit lots of jokes, others only one...

I want to display the newest joke submitted by each joker, in a list sorted
by newness
(using, say, the joke_id primary key)

I only want one joke to show from each joker

And I want every joker to be represented in the list

I am sure I once saw a way of sorting an array so that only one joke from
each joker remained...

Or am I having some sort of acid flashback?

Can anyone help?

TTTIA

Mark

PS reading this back it looks like I'm taking the piss, but I am actually
quite serious :0)

Jul 17 '05 #1
7 1341
SELECT DISTINCT joker, joke FROM jokes ORDER BY joke_id

Not sure if this works correctly, but i believe DISTINCT is your key to
succes

Jul 17 '05 #2
Mark wrote:

Say there is a database of jokes, from different jokers...
<snip>
I am sure I once saw a way of sorting an array so that only one joke from
each joker remained...


Kind of tricky to do with a PHP array. Easier to do with SQL (so not a PHP
question)...you could try

SELECT joker, MAX(date_submitted) as ident FROM jokes GROUP BY joker

Then to get the corresponding jokes, using the parameters retruned by the
above:

SELECT joke FROM jokes WHERE joker='$joker' AND date_submited='$ident'

This requires lots of queries though. It can be done with one. The
'simplest' way is to create a view based on the first query above...say
recent_jokes then:

SELECT recent.joker, ident, joke FROM recent,jokes
WHERE jokes.joker=recent.joker AND jokes.date_submitted=ident

But not all DBMS support views so....

SELECT joker, MAX(CONCAT(date_submitted,joke)) AS jokedata FROM jokes GROUP
BY joker

....which should also be the most efficient query. Obviously you'll need to
split jokedata in your code.

HTH

C.
Jul 17 '05 #3
Mark wrote:
Hi yall
Hi Mark,

I'm a newbie, forgive me...

In the absence of the Pope, I'll do the forgiving.
You are forgiven.
:-)

I am sure I have seen a simple way to do this, but I can't for the life of
me remember how it was done...

Say there is a database of jokes, from different jokers...

Some jokers submit lots of jokes, others only one...

I want to display the newest joke submitted by each joker, in a list
sorted by newness
(using, say, the joke_id primary key)
And the userid and the submitdate.
I take it you stored that too.

I only want one joke to show from each joker

And I want every joker to be represented in the list

I am sure I once saw a way of sorting an array so that only one joke from
each joker remained...
Well, many ways.

An easy way (but maybe not the fastest)
[postgresql style]

CREATE TABLE tbljokes(
jokeid SERIAL PRIMARY KEY,
submitdate date,
userid integer REFERENCES tbluser(userid),
thejoke text
)

Suppose you have the above table.

For sake of simplicity, let us assume that a higher jokeid means a later
submitdate.
Is that reasonable to assume?

Now you use something like:
SELECT userid, MAX(jokeid) AS latestjokeid FROM tbljokes
GROUP BY userid;

to get the latest jokeid per user.
OK?

Now we can use that trick to rewrite the query:
SELECT jokeid, submitdate, userid, thejoke
FROM tbljokes
WHERE ( jokeid IN
(SELECT jokeid FROM
(
SELECT userid, MAX(jokeid) AS latestjokeid FROM tbljokes
GROUP BY userid;
)
)
) ORDER BY submitdate;

Something like that.
I didn't test.


Or am I having some sort of acid flashback?
Nevermind them.

Go here and view the flash animation to get better. :P

http://odegaard.no-ip.com/morten/ind...pe=flash&menu=

Can anyone help?

TTTIA
You are welcome.

Regards,
Erwin Moller

Mark

PS reading this back it looks like I'm taking the piss, but I am actually
quite serious :0)


Jul 17 '05 #4
*Assuming you're using mySQL, for more info look in the mySQL function
list under DISTINCT.

Jul 17 '05 #5
Thanks to everyone for their responses...

I love you all.

I feel it's an appropriate time for a joke...

Q. What's the most difficult thing about rollerblading?

A. Telling your parents that you're gay.
(Obviously no offence is meant; either to rollerbladers, or homosexuals...
;0)
"Mark" <ha****@hahaha.com> wrote in message
news:1112780541.48bfd9a42ea6f85cf2187dbe854d5a94@t eranews...
Hi yall

I'm a newbie, forgive me...

I am sure I have seen a simple way to do this, but I can't for the life of
me remember how it was done...

Say there is a database of jokes, from different jokers...

Some jokers submit lots of jokes, others only one...

I want to display the newest joke submitted by each joker, in a list
sorted by newness
(using, say, the joke_id primary key)

I only want one joke to show from each joker

And I want every joker to be represented in the list

I am sure I once saw a way of sorting an array so that only one joke from
each joker remained...

Or am I having some sort of acid flashback?

Can anyone help?

TTTIA

Mark

PS reading this back it looks like I'm taking the piss, but I am actually
quite serious :0)

Jul 17 '05 #6
Hey uhhh . Did anyone read this article in here?

http://dev.mysql.com/tech-resources/...s/ddws/10.html

I think Mark did and is why he is asking this question. :)

Jul 17 '05 #7
My joke was much funnier than theirs (even without seeing the punchlines...
:0)

"llbbl" <ll***@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hey uhhh . Did anyone read this article in here?

http://dev.mysql.com/tech-resources/...s/ddws/10.html

I think Mark did and is why he is asking this question. :)

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
6946
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub ...
8
3226
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
6
2953
by: Chuck M | last post by:
Is there a simple way to change the sort order in Access (numeric-alpha) to alphanumeric? I've searched the help files and some books, but found no answer. Thanx for any advice Chuck
7
1252
by: Joe Fallon | last post by:
I have a WinForm that is a Base class. It has many controls on it and a lot of generic code behind. When I inherit the form I override 5 methods and now each child form has the same look and feel...
21
3168
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
2827
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
11
464
by: Jack | last post by:
Hi, I was asked this question during an interview. It seems very simple but I don't know if there is a more efficient way. The question said, we have 2 arrays as follows A = B = As you...
12
6967
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
5
1823
by: Chad | last post by:
I was looking at some old posts in comp.lang.c and found the following ...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.