If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > LIMIT, rand() and DISTINCT problems.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-21-06, 12:40
OddLaW OddLaW is offline
Registered User
 
Join Date: Feb 2005
Location: Oklahoma City, Oklahoma
Posts: 39
LIMIT, rand() and DISTINCT problems.

We are having problems displaying random results from our database.

THE WANTED OUTCOME:
We need to distinctivly randomly retrieve 3 results from our table.

THE CODE:
PHP Code:
$get_ads $mysqli->query("SELECT DISTINCT advertisers_id, id, image, pointurl, impressions, killimpressions, killdate FROM banners GROUP BY advertisers_id ORDER BY rand() LIMIT 3") or die($mysqli->error); 
THE PROBLEM:
The query is selecting the top 3 distinct results ... then randomizing them. We need the opposite. We need to select 3 random rows then limit them by 3.

MySQL Version 4.1

Please help! Thanks!!
__________________
www.m1k3.net
Reply With Quote
  #2 (permalink)  
Old 03-21-06, 13:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you must approach this problem from two different directions

first, let us discuss what you mean by distinct

since each banner has a distinct id, you are always going to get three distinct banners

so obviously, you must mean something else, right?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-21-06, 13:27
OddLaW OddLaW is offline
Registered User
 
Join Date: Feb 2005
Location: Oklahoma City, Oklahoma
Posts: 39
Whoops, I am sorry. I have two ID's. A banner ID (which is the primary key) and the advertisers_id (which gives relation to the advertisers table)

I need to select 3 distinct rows BY unique/distinct advertisers_id
__________________
www.m1k3.net
Reply With Quote
  #4 (permalink)  
Old 03-21-06, 14:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
we need to be a bit more precise

you want 3 banners, but each from a different advertiser, correct?

so it is the 3 advertisers that must be unique, not the banners
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On