473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql, ORDER BY ? GROUP BY ? please help me for a "hierarchical"q uery

I want to retrieve all comment stored from my web users ordered BY
THREAD and BY TIMESTAMP (INT 11) with a single query (if is possible)
now I launch this query:
SELECT thread, timestamp, level FROM comments WHERE articleid=16 ORDER
BY thread, timestamp,level

the result is
--------------------------

articleid

thread

timestamp

level
Edit Delete 16 0158c01d508b7f6 b439218b37b95c9 d8
1118615171 0
Edit Delete 16 08852417c915075 5dc79a227bdc141 66
1118614496 0
Edit Delete 16 1f36e5e49a9f63c 155da65d6b50cdf 57
1118700152 0
Edit Delete 16 21a893b9e131f93 fd74087aecd6e0a d9
1118615378 0
Edit Delete 16 23a08379da46d6c a93688bfd552a9f d2
1118616732 0
Edit Delete 16 26ddd774e9ed502 7e2b63ee76e07aa 63
1118614810 0
Edit Delete 16 2b8d2bc3a492113 3f4f66550ff1e0b 62
1118615722 0
Edit Delete 16 4ffac6f937c6277 10df83286427d5e 77
1118700068 0
Edit Delete 16 5599e5171abe3ff 1db3a1de96dd622 df
1118700205 0
Edit Delete 16 5abbc47f16ae3d5 e999d083357f672 26
1118617178 0
Edit Delete 16 683d385ec0d315b 34be5fab64b64cd 4a
1118700174 0
Edit Delete 16 97139b90f82c9d5 4df4c727ca59dfe e8
1118614046 0
Edit Delete 16 af32d2171a51c36 35513f77ecff3a7 cd
1118615830 0
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613704 0
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613714 1
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613734 2
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613744 1
Edit Delete 16 e10d9627b13b9e9 84dd8d3831cfc7c 10
1118615545 0
Edit Delete 16 h599e5171abe3ff 1db3a1de96dd622 df
1118613716 0
----------------------------
so I have ONE UNIQUE THREAD ( e0ab7e66c7dcb4e 50505d7b143f9bc cf ) but
these result are't ordered by timestamp !!!!
are apparently ordered only by Thread (alfabethically ).

there is the possibility to get the result ordered by TIMESTAMP and
aggregate the data by thread ?


if I make this:

SELECT thread, timestamp, level FROM comments WHERE articleid=16 ORDER
BY timestamp, thread, level
I have the result ordered by timestamp but not ordered by thread ( see
the third line )

--------------------------------
articleid

thread

timestamp

level
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613704 0
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613714 1
Edit Delete 16 h599e5171abe3ff 1db3a1de96dd622 df
1118613716 0
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613734 2
Edit Delete 16 e0ab7e66c7dcb4e 50505d7b143f9bc cf
1118613744 1
Edit Delete 16 97139b90f82c9d5 4df4c727ca59dfe e8
1118614046 0
Edit Delete 16 08852417c915075 5dc79a227bdc141 66
1118614496 0
-----------------------


i Have mysql version 4.0.x
so I haven't subquery and WITH ROLLUP OPTION too :-(


Can you help me ?
thanks a lot in advance !

Jul 23 '05 #1
1 2429
MultiTaskinG wrote:
there is the possibility to get the result ordered by TIMESTAMP and
aggregate the data by thread ?


I think you need to track which comment is "root" of each thread, and
then map each comment to its thread's root, JOIN to it in your query so
you can get the timestamp of the start of the thread.

To do this you could use two new columns in your table: one to uniquely
identify the comment (a primary key for the table), and another to
reference the root comment for the thread.

SELECT c.thread, c.timestamp, c.level
FROM comments AS c INNER JOIN comments AS root
ON c.thread_root = root.pk
WHERE c.articleid=16
ORDER BY root.timestamp, c.level, c.timestamp

Another way would be to add one new field, to mark a given comment as
the root of its thread. That might be easier; just set `is_root` to 1
if someone is posting a new message, or 0 if they're posting in reply to
another comment.

SELECT c.thread, c.timestamp, c.level
FROM comments AS c INNER JOIN comments AS root
ON c.thread = root.thread and root.is_root = 1
WHERE c.articleid=16
ORDER BY root.timestamp, c.level, c.timestamp

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

4
2744
by: bennett | last post by:
How can I do a loop from 1 to 5, where on line 1 I print 1 space followed by a button, on line 2 I print 2 spaces followed by a button, on line 3 I print 3 spaces followed by a button, etc.? I tried this code: for (int i = 1; i <= 5 ; ++i) { for (int j = 1; j <= i; ++j) {
20
6137
by: Troels Arvin | last post by:
Hello, In a rather large database, I face this problem: Several tables are regularly updated through rather long-running DELETE +INSERTs. Using uncommitted read as isolation level seems unsafe, as I'd like consistent data (that's one of the main points of using a DBMS in the first place, in my opinion). Almost all other significant databases have gotten MultiVersion Concurrency Control (MVCC) by now, either by default, or optionally
0
9353
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3889
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 we have to send another system
3
3481
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.