Jump to content

Need Help Writing A Sql


mcmuney

Recommended Posts

I'm not sure where to begin on this one. I'm working with a comment table and trying to determine who is writing the most "first" comment. For example, the table would have the followin fields:

 

id, comment, to_id, from_id

 

So what I need is to determine which 'from_id' is writing the most very first comment.

 

For example:

 

id, comment, to_id, from_id

1,abc,5,6 (6 is first)

2,bcd,5,7

3,lkd,9,7 (7 is first)

4,ljf,9,6

5,lsj,4,6 (6 is first)

6,lds,4,5 (5 is first)

 

In this example, from_id 6 would have a count of 2; 7 count of 1, 5 count of 1.

 

How would I write this statement?

Link to comment
https://forums.phpfreaks.com/topic/268764-need-help-writing-a-sql/
Share on other sites

A definitive answer depends on your database structure. For example, how are you determining what the "first" comment is?

 

You would likely use a COUNT() and group by the user ID. But, again, it depends on your database structure and how the "first" comment is determined.

There's no "first" indicator on the DB. It has to be determined by the 'id' i'm guessing? For example:

 

3,lkd,9,7 (7 is first)

4,ljf,9,6

 

If this is grouped by to_id and somehow pull the count based on the lowest id, is that possible?

 

I also have a date field if that's of any help.

I could be wrong, but it seems that both the 'date' and 'id' would serve the same purpose in determining the first. You can take the lowest id for each to_id or the earliest date.

 

The 'id' is never a good choice in determining the "first" post. In the beginning, yes, the AUTO INCREMENT field will be, well, auto incrementing; and lower id's will come before higher id's. But this is an arbitrary situation. If you ever have to reoganize the table or re-sequence the id's or delete older entries (with lower id's) and re-start the numbering; then the lower id will not be "first". You must first define what "first" means. If the definition of "first" is "earliest in time", then a DATETIME field should be used.

The 'id' is never a good choice in determining the "first" post. In the beginning, yes, the AUTO INCREMENT field will be, well, auto incrementing; and lower id's will come before higher id's. But this is an arbitrary situation. If you ever have to reoganize the table or re-sequence the id's or delete older entries (with lower id's) and re-start the numbering; then the lower id will not be "first". You must first define what "first" means. If the definition of "first" is "earliest in time", then a DATETIME field should be used.

Ah okay, so using the AUTO_INCREMENT column on the comment. Eh, I think using a date would be better.

 

I agree on the date comments, but the id is all the OP would give us to work with

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.