I have a query below. On executing it, it returns all the results where i can see multiple records for each single memberid.
I want to select the profiles of two members randomly chosen without any performance issue.
SELECT
a.memberid,
a.category_id,
a.content,
a.count_cid,
a.importance
FROM tb_profilingdata a,
tb_member b
WHERE a.memberid = b.memberid
AND a.category_id IN($catstr)
AND a.memberid NOT IN('$mid',$seen)
AND b.gender = 'male'
ORDER BY a.memberid, a.category_id
I tried some queries for choosing one random record
SELECT
r1.memberid
FROM tb_profilingdata AS r1
JOIN (SELECT
(RAND() * (SELECT MAX(DISTINCT(memberid)) FROM tb_profilingdata)) AS memberid) AS r2
WHERE r1.memberid >= r2.memberid
ORDER BY r1.memberid ASC
LIMIT 1
But it chooses in total 1 record out of tb_profilingdata whereas i want records of one randomly chosen member.
I tried the same query with tb_member, but it is possible that a member present in tb_member might not have its entries in tb_profilingdata..
Please suggest me a good way out with least performance issues.
May be that will help you:
SELECT [something] FROM [source] WHERE [conditions] ORDER BY RAND() LIMIT 2
This query is untested unless you provide some sample data
SELECT
a.memberid,
a.category_id,
a.content,
a.count_cid,
a.importance
FROM tb_profilingdata a
left join (select
memberid
from tb_member
group by memberid
order by rand()) b
on a.memberid = b.memberid
WHERE a.category_id IN($catstr)
AND a.memberid NOT IN('$mid',$seen)
AND b.gender = 'male'
ORDER BY a.memberid, a.category_id