I have two tables, rating:
+-----------+-----------+-------------+----------+
| rating_id | entity_id | rating_code | position |
+-----------+-----------+-------------+----------+
| 1 | 1 | Quality | 0 |
| 2 | 1 | Value | 0 |
| 3 | 1 | Price | 0 |
+-----------+-----------+-------------+----------+
And rating_option
+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 | 2 |
| 3 | 1 | 3 | 3 | 3 |
| 4 | 1 | 4 | 4 | 4 |
| 5 | 1 | 5 | 5 | 5 |
| 6 | 2 | 1 | 1 | 1 |
| 7 | 2 | 2 | 2 | 2 |
| 8 | 2 | 3 | 3 | 3 |
| 9 | 2 | 4 | 4 | 4 |
| 10 | 2 | 5 | 5 | 5 |
| 11 | 3 | 1 | 1 | 1 |
| 12 | 3 | 2 | 2 | 2 |
| 13 | 3 | 3 | 3 | 3 |
| 14 | 3 | 4 | 4 | 4 |
| 15 | 3 | 5 | 5 | 5 |
+-----------+-----------+------+-------+----------+
I need a SQL query (not application level, must stay in the database) which will select a set of ratings randomly. A sample result would look like this, but would pick a random value for each rating_id on subsequent calls:
+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 8 | 2 | 3 | 3 | 3 |
| 15 | 3 | 5 | 5 | 5 |
+-----------+-----------+------+-------+----------+
I'm totally stuck on the random part, and grouping by rating_id has been a crap shoot so far. Any MySQL ninjas want to take a stab?
Thanks,
Joe
EDIT: I've tried rand() in a bunch of combinations, and I'm sure that it will be necessary to create the randomness of the result, but I cannot figure out how to return one random row for each of the rows in rating. I cannot use order by rand() limit 1 because I need three rows, and order by rand() limit 3 won't give me one of each rating_id, which is the ultimate goal. I need a combination of rand() and either subqueries or joins so that I can ensure one of each rating_id.
Alright, a little messy, but seems to do the job. Someone may know what they're doing better than I do that can clean this up:
SELECT random.rating_id, random.rand_option_id, r3.code, r3.value, r3.position
FROM
(SELECT r.rating_id,
(SELECT r2.option_id
FROM rating_option r2
WHERE r2.rating_id = r.rating_id
ORDER BY RAND() LIMIT 1) AS 'rand_option_id'
FROM rating_option r
GROUP BY r.rating_id
) random
LEFT JOIN rating_option AS r3 ON r3.option_id = rand_option_id
Results (varies every time, of course):
+-----------+----------------+------+-------+----------+
| rating_id | rand_option_id | code | value | position |
+-----------+----------------+------+-------+----------+
| 1 | 4 | 4 | 4 | 4 |
| 2 | 6 | 1 | 1 | 1 |
| 3 | 13 | 3 | 3 | 3 |
+-----------+----------------+------+-------+----------+
You could use the rand() function to do sorting in a select on the rating table.
For example:
select rating_id from rating order by rand() limit 1
As clarified in your comments, and the other posts above
select * from rating_option order by rand()
will return all records in a random order... However, if you want only X number, then inclue that as the limit as noted by others
select * from rating_option order by rand() limit 5 (or whatever number)
Have you looked into the rand() function?
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
http://www.petefreitag.com/item/466.cfm
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
Sample code
select *
from rating_option
group by rating_id
order by rand()
Related
I have a MySQL table like this:
+------+--------+--------+
| ID | UserID | Score |
+------+--------+--------+
| 1 | 3 | 12 |
| 2 | 3 | 11 |
| 3 | 3 | 12 |
| 4 | 2 | 14 |
| 5 | 4 | 8 |
| 6 | 2 | 13 |
+------+--------+--------+
From this I want to top 3 scores from entire table and a top score from particular user
+------+--------+--------+
| ID | UserID | Score |
+------+--------+--------+
| 4 | 2 | 14 |
| 6 | 2 | 13 |
| 3 | 3 | 12 |
| 5 | 4 | 8 |
+------+--------+--------+
Is this something I can get done in a single query?
Any help is appreciated
Thanks in advance!
spoiler alert ;)
(SELECT * FROM tableA ORDER BY score DESC LIMIT 3)
UNION
(SELECT * FROM tableB WHERE UserID = 4 ORDER BY score DESC LIMIT 1);
For certain definitions of "single query", sure... If you're willing to use sub queries or a union.
The best performance will probably come from two queries, but you can join them in a UNION for convenience if you wish.
I want a query that selects all rows that have the UploadedbyUserID = Rand() (selects random id from possible UploadbyUserID in this case 4, 3 and 22 and only those 3 not 2 nor 5)
And if the rand gives 4 it outputs this:
+------+------+------------+--------------------+
| id | name | date | UploadedbyUserID |
+------+------+------------+--------------------+
| 1 | 2222 | Testing | 4 |
| 2 | Jack | description| 4 |
| 6 | Zara | 2007-02-06 | 4 |
+------+------+------------+--------------------+
This is the whole table
+------+------+------------+--------------------+
| id | name | date | UploadedbyUserID |
+------+------+------------+--------------------+
| 1 | 2222 | Testing | 4 |
| 2 | Jack | description| 4 |
| 3 | ffdsd| 2007-05-06 | 4 |
| 4 | dsm | 2007-05-27 | 3 |
| 5 | dddd | 2007-04-06 | 3 |
| 6 | Zara | 2007-02-06 | 4 |
| 7 | John | 2007-01-24 | 22 |
+------+------+------------+--------------------+
and if it randomizes 3 it outputs this
+------+------+------------+--------------------+
| id | name | date | UploadedbyUserID |
+------+------+------------+--------------------+
| 4 | dsm | 2007-05-27 | 3 |
| 5 | dddd | 2007-04-06 | 3 |
+------+------+------------+--------------------+
Ask if you need more information
Hmmm. This is one way:
select t.*
from (select uploadedbyuserid
from t
order by rand()
limit 1
) u join
t
using (uploadedbyuserid);
First, let me say that this is weighted by the number of times that a user has uploaded something. So, user "4" would appear a bit more often than "3", in your example. If this is an issue:
select t.*
from (select uploadedbyuserid
from (select distinct uploadedbyuserid from t) t
order by rand()
limit 1
) u join
t
using (uploadedbyuserid);
The next observation is that this can be compute intensive. If you have lots of rows, there are various ways to speed these up. For instance, one simple method would be to get about 1 out of 10000 rows:
select t.*
from (select uploadedbyuserid
from (select distinct uploadedbyuserid
from t
) t
where rand() < 0.001
order by rand()
limit 1
) u join
t
using (uploadedbyuserid);
i am loosing it over the following problem:
i have a table with participants and points. each participant can have up to 11 point entries of which i only want the sum of the top 6.
in this example lets say we want the top 2 of 3
+----+---------------+--------+
| id | participantid | points |
+----+---------------+--------+
| 1 | 1 | 11 |
+----+---------------+--------+
| 2 | 3 | 1 |
+----+---------------+--------+
| 3 | 3 | 4 |
+----+---------------+--------+
| 4 | 2 | 3 |
+----+---------------+--------+
| 5 | 1 | 5 |
+----+---------------+--------+
| 6 | 2 | 10 |
+----+---------------+--------+
| 7 | 2 | 9 |
+----+---------------+--------+
| 8 | 1 | 3 |
+----+---------------+--------+
| 9 | 3 | 4 |
+----+---------------+--------+
as a result i want something like
+---------------+--------+
| participantid | points |
+---------------+--------+
| 2 | 19 |
+---------------+--------+
| 1 | 16 |
+---------------+--------+
| 3 | 8 |
+---------------+--------+
(it should be ordered DESC by the resulting points)
is this at all possible with mysql? in one query?
oh and the resulting participant ids should be resolved into the real names from another 'partcipant' table where
+----+------+
| id | name |
+----+------+
| 1 | what |
+----+------+
| 2 | ev |
+----+------+
| 3 | er |
+----+------+
but that should be doable with a join at some point... i know...
Using one of the answers from ROW_NUMBER() in MySQL for row counts, and then modifying to get the top.
SELECT ParticipantId, SUM(Points)
FROM
(
SELECT a.participantid, a.points, a.id, count(*) as row_number
FROM scores a
JOIN scores b ON a.participantid = b.participantid AND cast(concat(a.points,'.', a.id) as decimal) <= cast(concat(b.points,'.', b.id) as decimal)
GROUP BY a.participantid, a.points, a.id
) C
WHERE row_number IN (1,2)
GROUP BY ParticipantId
Had an issue with ties until I arbitrarily broke them with the id
Suppose I have such a table:
+-----+---------+-------+
| ID | TIME | DAY |
+-----+---------+-------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 2 |
| 1 | 1 | 3 |
| 2 | 2 | 3 |
| 3 | 3 | 3 |
| 1 | 1 | 4 |
| 2 | 2 | 4 |
| 3 | 3 | 4 |
| 1 | 1 | 5 |
| 2 | 2 | 5 |
| 3 | 3 | 5 |
+-----+---------+-------+
I want to fetch a table which represents 2 IDs which got the largest sum of TIME within the last 3 days (means from 3 to 5 in a DAY column)
So the correct result would be:
+-----+---------+
| ID | SUM |
+-----+---------+
| 3 | 9 |
| 2 | 6 |
+-----+---------+
The original table is much larger and more complex. So i need a generic approach.
Thanks in advance.
And so I just learned that MySQL used LIMIT instead of TOP...
fiddle
CREATE TABLE tbl (ID INT,tm INT,dy INT);
INSERT INTO tbl (id, tm, dy) VALUES
(1,1,1)
,(2,2,1)
,(3,3,1)
,(1,1,2)
,(1,1,1)
SELECT ID
,SUM(SumTimeForDay) SumTimeFromLastThreeDays
FROM (SELECT ID
,SUM(tm) SumTimeForDay
FROM tbl
GROUP BY ID, dy
HAVING dy > MAX(dy) -3) a
GROUP BY id
ORDER BY SUM(SumTimeForDay) DESC
LIMIT 2
select t1.`id`, sum(t1.`time`) as `sum`
from `table` t1
inner join ( select distinct `day` from `table` order by `day` desc limit 3 ) t2
on t2.`da`y = t1.`day`
group by t1.`id`
order by sum(t1.`time`) desc
limit 2
I have a table called lottery_winners with the following useful colums:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
id is the unique, primary id for the table. plid refers to the past_lotteries table (i.e I can get all the lottery winners from a specific lottery in the past this way. zbid is the id of the member/user (the winner). amount is the sum of money they won in the lottery, and finally numbers is a VARCHAR CSV field with their lottery numbers.
Here's an example of what rows could be in the table:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 5 | 2 | 2 | 8 | 8,9,10 |
+----+------+------+--------+---------+
Now, I want to run a SELECT statement which will bring back all the rows but in a really specific order. The rows should be in grouped by zbid as such (in this case I have added a WHERE plid=1 clause):
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
Next criteria is that not only should they be grouped by zbid, but within this grouping they should be ordered by amount DESC. This is what it would now look like:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
The top two rows have swapped around.
One more criteria. As you can see, although they are grouped by zbid, there's no specific order to them. I want it to group by zbid, but the order should be based on sum(amount) for each group.
The following table shows the totals for each zbid in no specific order (taking into account that plid=1:
+------+-------------+
| zbid | sum(amount) |
+------+-------------+
| 2 | 4 |
+------+-------------+
| 3 | 7 |
+------+-------------+
| 4 | 5 |
+------+-------------+
So using this information the final result using the SELECT statement should be the following (with an added sum(amount) column):
+----+------+------+--------+---------+-------------+
| id | plid | zbid | amount | numbers | sum(amount) |
+----+------+------+--------+---------+-------------+
| 3 | 1 | 3 | 7 | 3,4,5 | 7 |
+----+------+------+--------+---------+-------------+
| 2 | 1 | 4 | 5 | 4,5,6 | 5 |
+----+------+------+--------+---------+-------------+
| 4 | 1 | 2 | 3 | 7,8,9 | 4 |
+----+------+------+--------+---------+-------------+
| 1 | 1 | 2 | 1 | 1,2,3 | 4 |
+----+------+------+--------+---------+-------------+
That's it! Now I've tried a couple of things myself, but I'm not exactly sure how to get the full final result. I have tried:
SELECT id,plid,zbid,amount,numbers,sum(amount) FROM lottery_winners GROUP BY zbid ORDER BY sum(amount) DESC
Now that seemed to meet the final criterion, but it didn't give me individual results for the table.
Please also note that as these results will be paginated, I will need to be adding LIMIT $start,$perpage to the end of the query.
have this a try:
SELECT a.id,
a.plid,
a.zbid,
a.amount,
a.numbers,
c.totalAmount
FROM lottery_winners a
INNER JOIN (
SELECT b.zbid,
SUM(b.amount) totalAmount
FROM lottery_winner b
WHERE b.plid = 1
GROUP BY b.zbid
) c
ON a.zbid = c.zbid
WHERE a.plid = 1
ORDER BY c.totalAmount desc