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
How to write this query?
Old 10-09-2008, 11:07 AM How to write this query?
Junior Talker

Posts: 3
Trades: 0
Hello, guys. On my web project I have this query:

Code:
SELECT news_store.id, news_store.title, news_store.message, news_store.date, news_store.time, news_store.file_name,
                                    news_regions.title AS region, news_regions.path AS region_path,
                                    news_categories.title AS category, news_categories.path AS category_path,
                                    news_hosts.host AS host
                                    FROM news_store, news_regions, news_categories, news_hosts
                                    WHERE news_store.cat_id IN (1,3,6,9,11,13,17,44,51,54,76,88)
                                    AND news_regions.id = news_store.geo_id
                                    AND news_categories.id = news_store.cat_id
                                    AND news_hosts.id = news_store.src_id
                                    ORDER BY news_store.id DESC LIMIT 0, 3
Problem is that I need 3 records with 3 DIFFERENT news_store.src_id's, but when I try to put in code "GROUP BY news_store.src_id" after ORDER BY statement I have MySQL error, and if I put this code before ORDER BY I have incorrect result data, coz rows first grouped and only then sorted . Please, help, I'm going mad with this query...
genykm is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-09-2008, 01:16 PM Re: How to write this query?
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Try using the DISTINCT keyword?
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 10-09-2008, 07:32 PM Re: How to write this query?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
You're not writing an aggregate query, so you can't group by a single column value, without telling the database how to aggregate all the other columns, or adding them also to your group by clause.
__________________

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


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 10-10-2008, 06:27 AM Re: How to write this query?
Junior Talker

Posts: 3
Trades: 0
nyef, thanks!!! I've tried DISTINCT many times, but always got some errors, I've tried it now and all seems like working, may be I have made some errors in my previous queries, thanks!
genykm is offline
Reply With Quote
View Public Profile
 
Old 10-10-2008, 08:13 AM Re: How to write this query?
Junior Talker

Posts: 3
Trades: 0
Nope... It seems like working, but NOT. DISTINCT keyword is used to ALL fields, so if my row id is always different, MySQL think that rows are different even src_id is same, but I need exactly rows with different src_id...
genykm is offline
Reply With Quote
View Public Profile
 
Old 10-11-2008, 04:26 PM Re: How to write this query?
Ultra Talker

Posts: 310
Trades: 0
I think you can use group_concat to do what you are trying to do.
dman_2007 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to write this query?
 

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.76517 seconds with 12 queries