jeff5656 Posted August 9, 2011 Share Posted August 9, 2011 I want to select records with DISTINCT only records with payment_type of a certain type are displayed once: $query = "select distinct payment_type from payments "; So if I have payment_types of "A", "B" and "C" each of those 3 will only be displayed once and not repeated. The problem is, how do I SELECT more fields from each record? I tried $query = "select distinct payment_type, * from payments "; but that didn't work. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/ Share on other sites More sharing options...
Maq Posted August 9, 2011 Share Posted August 9, 2011 Use GROUP BY: $query = "SELECT * FROM payments GROUP BY payment_type"; Also, don't use * unless you really do need all the columns. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255043 Share on other sites More sharing options...
The Little Guy Posted August 9, 2011 Share Posted August 9, 2011 Also, don't use * unless you really do need all the columns. I hear that is also a good idea to list the fields in the order they are in the database skipping the ones not needed. I could be wrong but that is what I heard. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255051 Share on other sites More sharing options...
Maq Posted August 9, 2011 Share Posted August 9, 2011 Also, don't use * unless you really do need all the columns. I hear that is also a good idea to list the fields in the order they are in the database skipping the ones not needed. I could be wrong but that is what I heard. Haven't heard of that, do you know for what reason(s)? Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255054 Share on other sites More sharing options...
The Little Guy Posted August 9, 2011 Share Posted August 9, 2011 I was told it was faster. I assume it is so MySQL doesn't have to reorder columns/data, it would just get the columns/data as is without having to reorder it. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255060 Share on other sites More sharing options...
Maq Posted August 9, 2011 Share Posted August 9, 2011 I was told it was faster. I assume it is so MySQL doesn't have to reorder columns/data, it would just get the columns/data as is without having to reorder it. Interesting. I guess it makes sense, I'll have to check it out. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255063 Share on other sites More sharing options...
fenway Posted August 9, 2011 Share Posted August 9, 2011 Yes, but not noticeably so. Also, don't mix * and GROUP BY -- you'll get garbage data. Selecting "more" with one of each payment type doesn't make any sense. Link to comment https://forums.phpfreaks.com/topic/244355-using-distinct/#findComment-1255066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.