dbTalk Databases Forums  

Need help getting unique values

comp.databases.mysql comp.databases.mysql


Discuss Need help getting unique values in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Adrienne Boswell
 
Posts: n/a

Default Need help getting unique values - 05-12-2011 , 11:30 PM






Merchant table: 253 merchants - each merchant has one primary category
Category table: 161 categories with images (one image per category)
Merchants pick one primary category, some categories are more popular
than others, for instance the Dry Cleaners has the most merchants at 13.

I need to pick out 2 different merchants, not in the same category, and
put them in another table, so they will not get selected again for 30
days.

Right now, I'm doing something like the below but I'm getting dupes:


SELECT merchant, category, imgsrc
FROM merchants
JOIN category ON merchants.pricategory_id = category.id
ORDER BY RAND() <-- this is where the difficulty is
WHERE merchant NOT IN
(SELECT merchant
FROM usedmerchants
WHERE DATEDIFF(CURDATE(),added) < 30
)
LIMIT 2


If the entire table schemas are needed, I will be a happy to post them.
TIA.


--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Need help getting unique values - 05-13-2011 , 02:03 PM






On 13-05-2011 06:30, Adrienne Boswell wrote:
Quote:
Merchant table: 253 merchants - each merchant has one primary category
Category table: 161 categories with images (one image per category)
Merchants pick one primary category, some categories are more popular
than others, for instance the Dry Cleaners has the most merchants at 13.

I need to pick out 2 different merchants, not in the same category, and
put them in another table, so they will not get selected again for 30
days.

Right now, I'm doing something like the below but I'm getting dupes:


SELECT merchant, category, imgsrc
FROM merchants
JOIN category ON merchants.pricategory_id = category.id
ORDER BY RAND() <-- this is where the difficulty is
Why do you think there's a difficulty here?

Quote:
WHERE merchant NOT IN
(SELECT merchant
FROM usedmerchants
WHERE DATEDIFF(CURDATE(),added) < 30
)
LIMIT 2


If the entire table schemas are needed, I will be a happy to post them.
TIA.


If i had to do this i would select one merchant at a time, and update
the 'added' directly to avoid this merchang from being chosen again
(within the next 30 days)

When picking the second merchant you can add something to the
WHERE-clause to avoid picking a second-merchant from the same category
as the first merchant. ( Which, as i read it, is not wat you want.)

--
Luuk

Reply With Quote
  #3  
Old   
Luuk
 
Posts: n/a

Default Re: Need help getting unique values - 05-13-2011 , 02:14 PM



On 13-05-2011 21:03, Luuk wrote:
Quote:
On 13-05-2011 06:30, Adrienne Boswell wrote:
ORDER BY RAND() <-- this is where the difficulty is

Why do you think there's a difficulty here?

21:13:03 root@test[10]mysql> select min(i),max(i),count(i) from ints;
+--------+--------+----------+
Quote:
min(i) | max(i) | count(i) |
+--------+--------+----------+
0 | 32 | 32 |
+--------+--------+----------+
1 row in set (0.00 sec)

21:13:18 root@test[11]mysql> select * from ints order by rand() limit 5 ;
+----+
Quote:
i |
+----+
24 |
0 |
2 |
30 |
23 |
+----+
5 rows in set (0.00 sec)

21:13:22 root@test[12]mysql> select * from ints order by rand() limit 5 ;
+----+
Quote:
i |
+----+
29 |
19 |
27 |
23 |
32 |
+----+
5 rows in set (0.00 sec)

The RAND() function seems to do a good job in getting random results

--
Luuk

Reply With Quote
  #4  
Old   
Luuk
 
Posts: n/a

Default Re: Need help getting unique values - 05-13-2011 , 03:00 PM



On 13-05-2011 06:30, Adrienne Boswell wrote:
Quote:
Merchant table: 253 merchants - each merchant has one primary category
Category table: 161 categories with images (one image per category)
Merchants pick one primary category, some categories are more popular
than others, for instance the Dry Cleaners has the most merchants at 13.

I need to pick out 2 different merchants, not in the same category, and
put them in another table, so they will not get selected again for 30
days.

Right now, I'm doing something like the below but I'm getting dupes:


SELECT merchant, category, imgsrc
FROM merchants
JOIN category ON merchants.pricategory_id = category.id
ORDER BY RAND() <-- this is where the difficulty is
WHERE merchant NOT IN
(SELECT merchant
FROM usedmerchants
WHERE DATEDIFF(CURDATE(),added) < 30
)
LIMIT 2



or:

SELECT merchant, category, imgsrc
FROM merchants
JOIN category ON merchants.pricategory_id = category.id
INNER JOIN usedmerchants ON merchants.pricategory_id = usedmerchants.id
AND DATEDIFF(CURDATE(),added) > 30
ORDER BY RAND()
LIMIT .....
You still need to select 1 at a time, because the second one should have
a different category...


--
Luuk

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.