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
https://forums.phpfreaks.com/topic/233223-select-distinct-field/
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)

  • 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!

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.