Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
ORDER BY problem MySQL
Old 08-02-2008, 05:20 AM ORDER BY problem MySQL
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Here is my SQL query:

Code:
SELECT tblPortImg.portId, tblPortImg.portImgFile, tblPort.portId, tblPort.portTitle
                            FROM tblPortImg, tblPort
                            WHERE tblPortImg.portId = tblPort.portId
                            AND portType = '$portType'
                            GROUP BY tblPortImg.portId
                            ORDER BY tblPort.portTitle
Everything works fine, but the bit:

Code:
GROUP BY tblPortImg.portId
I need it so that it selects only the first record of tblPortImg.portImgId, whilst sort of keeping everything the same.

Is there a simple way to do it? Or have I not given enough information?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 08-02-2008, 07:01 AM Re: ORDER BY problem MySQL
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm not sure you have understood what group by does, or maybe I haven't understood your question...

"Group by" is an aggregate function, that will group all similar values as one.
Often, you use it with an "using" statement, which allows, for example, to count how many time a certain value is present and to display only those who are present multiple times
Code:
select count(username),username
from users
group by username
having count(username)>1
If you want the first (or any) value of the table, and if you use mysql or postgresql, you can use the "limit x offset y" statement.
Code:
select *
from tbl
limit 1 offset 0
And finally, if you want every different values, you can use a "select distinct".
Code:
select distinct username
from users
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-02-2008, 08:22 AM Re: ORDER BY problem MySQL
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks.

I think I understand what you mean. Im using GROUP BY to select only one record from the field

tblPortImg.portImgFile

for each record from tblPort.portId (if that makes sense?) I was thinking originally that I should use SELECT DISTINCT, but I couldnt figure out how exactly, then someone told me to use GROUP BY

Do you know how I could achieve this with SELECT DISTINCT ?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 08-02-2008 at 08:25 AM..
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 08-16-2008, 03:44 AM Re: ORDER BY problem MySQL
Novice Talker

Posts: 5
Name: Anthony Pham
Trades: 0
Cannot be achieved with distinct because you select multiple fields in select statement. Could you please tell me what exactly do you want? so I can helps.
__________________

Please login or register to view this content. Registration is FREE
webburu is offline
Reply With Quote
View Public Profile Visit webburu's homepage!
 
Reply     « Reply to ORDER BY problem MySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53264 seconds with 12 queries