Jump to content

Select Distinct field


cjackson111

Recommended Posts

Hello.

 

I am trying to display only one instance of records that have the same memberid in my db. I am using the following statement but it continues to show all of the records that have the same memberid. Any ideas what I may be doing wrong?

 

$sql = "select DISTINCT memberid, event, category, date, enddate, locality, location, address, city, state, zip, contact, phone, notes, doc1, doc2, doc3, doc4, doc5 from event where date >= '$datenow' ORDER by date ASC";

 

Thanks for any help!

Link to comment
Share on other sites

All, then use GROUP BY.

 

When you use distinct, if ANY of the other fields are different for that one memberid, it is considered distinct.

 

For example, I have two number 1 id's but they have different names.  therefore they are both listed.

 

mysql> SELECT DISTINCT id, name FROM test;
+----+-------+
| id | name  |
+----+-------+
|  1 | cat   |
|  1 | dog   |
|  2 | bird  |
|  3 | horse |
+----+-------+

 

So I use group and here is the outcome:

 

mysql> SELECT id, name FROM test GROUP BY id;
+----+-------+
| id | name  |
+----+-------+
|  1 | cat   |
|  2 | bird  |
|  3 | horse |
+----+-------+
3 rows in set (0.00 sec)

Link to comment
Share on other sites

I only want to show one of them

 

Yes, but which one? The first one in the table, last one in the table, oldest one, newest one, longest one, shortest one, average of them, sum of them?

 

Computers need to be told exactly what to do.

Link to comment
Share on other sites

  • 2 weeks later...

Ok, I have a little snag. Everything works great except for the last bit (order by date ASC). It still shows whatever record is first in the database. Any ideas what I may be doing wrong?

 

$sql = "select memberid, event, category, date, enddate, locality, location, address, city, state, zip, contact, phone, notes, doc1, doc2, doc3, doc4, doc5 from event where date >= '$datenow' GROUP BY memberid ORDER BY date ASC";

 

Thanks!

Link to comment
Share on other sites

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.