Solved

DISTINCTROW Problem

Posted on 2004-08-17
7
498 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now