Solved

DISTINCTROW Problem

Posted on 2004-08-17
7
501 Views
Last Modified: 2013-12-12
Here is my query:

SELECT Distinctrow SUBCAT From $tablename Where PRODUCTTYPE = 'widgets' ORDER BY SUBCAT

I am trying to select the entire row but this is only returning the SUBCAT column.  The query is in a .php file and pulling from a MySQL database
0
Comment
Question by:dspavlik
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 17

Expert Comment

by:akshah123
ID: 11827079
Try:

SELECT Distinctrow * From $tablename Where PRODUCTTYPE = 'widgets' ORDER BY SUBCAT
0
 

Author Comment

by:dspavlik
ID: 11827108
That is returning too many records.
0
 
LVL 9

Expert Comment

by:e-tsik
ID: 11827512
Hi :-)

Selecting all columns present a problem. Basically, it is:

SELECT * From $tablename Where PRODUCTTYPE = 'widgets'
GROUP BY SUBCAT

Read the GROUP BY reference on mysql, you may want to aggregate some of your columns with MIN,AVG,SUM, etc.

(just an example)
SELECT SUBCAT, MIN(PRICE), MAX(SCORE) From $tablename Where PRODUCTTYPE = 'widgets'
GROUP BY SUBCAT

Enjoy!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dspavlik
ID: 11829615
Ok maybe I am not being clear.

In my query:
SELECT Distinctrow SUBCAT From $tablename Where PRODUCTTYPE = 'widgets' ORDER BY SUBCAT
 
I am trying to select the distinctrow based on the what is in the SUBCAT column and only selet those distinctrows if the PRODUCTTYPE = 'widgets'

What is being returned is just the distinct values from just the SUBCAT column, not all the columns in the table.

There has got to be an easy way to do this.....


Thanks
0
 
LVL 9

Expert Comment

by:e-tsik
ID: 11834387
I read your sentence 3 times

SELECT * From $tablename Where PRODUCTTYPE = 'widgets'
GROUP BY SUBCAT ORDER BY SUBCAT

SELECT *   -->  Select all columns
FROM $tablename --> From $tablename (what's so secret about that table??)
WHERE PRODUCTTYPE='widgets'   --->  "only selet those distinctrows if the PRODUCTTYPE = 'widgets'"
GROUP BY SUBCAT --> "select the distinctrow based on the what is in the SUBCAT column"
ORDER BY SUBCAT --> If you fancy it, why should I stand in your way?
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11835242
"Ok maybe I am not being clear."

No, you are being very clear. However, you are not reading the replys very well. Your question has been answerd correctly and then explained.... twice.

SELECT Distinctrow * From $tablename Where PRODUCTTYPE = 'widgets' ORDER BY SUBCAT

That query does exactly what you asked. However, I think you are not asking for what you actually need.

"That is returning too many records."

As explained by e-tsik, this is because you are asking for the entire row, which means you will get a record returned for every unique value of each field not being grouped by. The way to resolve this, also already explained by e-tsik, is to employ aggregate funtions such as min,max,avg,sum,etc. on said fields.

If you post a few of the records returned that caused you to make the statement "That is returning too many records.", then I will try to explain further using those records as a visual example to illustrate what is going on.


Alan


0
 
LVL 9

Accepted Solution

by:
AlanJDM earned 500 total points
ID: 11835290
try...

SELECT Distinctrow * From $tablename Where PRODUCTTYPE = 'widgets' group by SUBCAT

instead of

SELECT Distinctrow * From $tablename Where PRODUCTTYPE = 'widgets' ORDER BY SUBCAT

and see if that is what you are looking for.


Alan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question