Laravel Eloquent : Select random rows on relation based - php

I want to pick questions randomly but category dependent. For example, if the test is given with 10 questions and the total category is 5, the test flow should take 2 questions randomly from each category. Is there a way to select it through random and eloquent relations?
and the question table
+-------+-------+-------+-------+
| id | category_id |.......|
+-------+-------+-------+-------+
already I am using random eloquent but the probability of getting questions from each category is low
public getRandomQuestions($limit)
{
$this->inRandomOrder()->limit($limit)->get()
}
and I'm clueless when it's coming to relations.

You can also use the inRandomOrder and groupBy method together to select random questions from each category.
$questions = Question::with('category')->inRandomOrder()->groupBy('category_id')->limit(2)->get();
This will give you 2 random questions from each category.
You can also use subquery to select questions with certain number of random questions per category
$questions = Question::with('category')
->whereIn('category_id', function($query) use ($limit) {
$query->select('category_id')
->from('questions')
->groupBy('category_id')
->inRandomOrder()
->limit($limit)
})->get();

the query to get 1 random question for each category:
SELECT *
FROM
(SELECT *,
#position := IF(#current_cate=category_id, #position + 1, 1) AS POSITION,
#current_cate := category_id
FROM
(SELECT q.*
FROM category c
INNER JOIN question q ON c.id = q.category_id
ORDER BY RAND()) temp
ORDER BY category_id) temp1
WHERE POSITION <= 2
ORDER BY category_id;
explanation:
since you want the question to be take randomly we need order by rand(), note: inRandomOrder also uses order by rand() under the hood
to be able to get 2 questions for each category, we need a variable (#position) to mark the order of question
laravel implementation:
public getRandomQuestions($limit)
{
$questions = DB::select("SELECT *
FROM
(SELECT *,
#position := IF(#current_cate=category_id, #position + 1, 1) AS POSITION,
#current_cate := category_id
FROM
(SELECT q.*
FROM category c
INNER JOIN question q ON c.id = q.category_id
ORDER BY RAND()) temp
ORDER BY category_id) temp1
WHERE POSITION <= 2
ORDER BY category_id");
return Question::hydrate($questions->toArray());
}

If you're using PHP >= 7.2 the use shuffle()
public function getRandomQuestions($limit) {
return Question::limit($limit)->groupBy('category_id')->get()->shuffle();
}
or else
public function getRandomQuestions($limit) {
return Question::inRandomOrder()->limit($limit)->groupBy('category_id')->get();
}
the trick is you need to use groupBy() clause for this

Related

Sorting data from MySQL by SUM with Grouping By - not working properly

I have a problem with mysql - i'm kind new to it, byt looking to improve my skills :)
Have a code like this:
if($where > 0) $query = mysql_query("SELECT img.*, user.user as owner_name, cat.name as cat_name FROM tentego_img AS img LEFT JOIN tablicacms_users AS user ON user.id = img.owner LEFT JOIN tentego_img_cat AS cat ON cat.id = img.cat WHERE img.`is_waiting` LIKE ".$where.$cat." INNER JOIN tentego_img_vote ON tentego_img.id = tentego_img_vote.object_id GROUP BY tentego_img_vote.object_id ORDER BY SUM ( (CASE WHEN tentego_img_vote.vote = '0' THEN '-1' ELSE '1' END) ) DESC LIMIT ".$page.",".$objPerPage);
I need to make sorting by number of votes, sorted descending.
Still it makes results sorted by it own way.
In table I have rows:
ID - vote id for table purpose
object_id- id of object joined with another table to show results.
User ID - user id
Vote - where values are 0 for dislike and 1 for like (so -1 for 0, and +1 for 1)
So, as I understand i need to sum up all records for each of unique object_id, then sort by sum of vote values of each.
This code worked before my script provider decide to upgrade it, so right now i dont know how to fix it :(

Better way than current query to assemble random categorized entries?

I am trying to display exactly 6 random 'entertainment' entries, but with my current query it's getting a random number between 1 and 6, and displaying that number of entries. How do I update this query in order to make it display exactly 6 random entertainment entries from my Articles table? Also, I don't want to do ORDER BY RAND() because my table will become bigger overtime. Here's my current query:
SELECT
r1.*
FROM
Articles AS r1
INNER JOIN (SELECT(RAND() * (SELECT MAX(id) FROM Articles)) AS id) AS r2
WHERE
r1.id >= r2.id
AND r1.category = 'entertainment'
LIMIT 6;
Table structure:
table Articles
- id (int)
- category (varchar)
- title (varchar)
- image (varchar)
- link (varchar)
- Counter (int)
- dateStamp (datetime)
Your 'entertainment' entries should all have unique id's which should be integers.
If this is the case you could generate 6 random int's between 1 and the amount of entries you have using PHP's rand() function. Here is a function I've written which may be useful.
function selectSixRandomEntries() {
$queryWhere = "";
$i = 0;
while($i < 6) {
$randomNumber = rand(1, 200);
if (strpos($queryWhere, $randomNumber) == -1)
continue;
$queryWhere .= "r1.id = " . rand(1, 200);
if ($i != 5)
$queryWhere .= " OR ";
$i++;
}
return $queryWhere
}
And to use it you could try
$query = "SELECT
r1.*
FROM
Articles AS r1
INNER JOIN (SELECT(RAND() * (SELECT MAX(id) FROM Articles)) AS id) AS r2
WHERE
" . selectSixRandomEntries() . "
AND r1.category = 'entertainment'
LIMIT 6";
With
select floor(rand() * m.maxId + 1) as randomId
from Articles a
join (SELECT MAX(id) maxId FROM Articles) m
limit 100
you will create 100 random ids. I take 100 because you have gaps in you id column, so the probability of not getting enough existing ids will be (very) small. Then you can use that result to select only 6 rows with those ids:
select distinct a.*
from (
select id, floor(rand() * m.maxId + 1) as randomId
from Articles a
join (SELECT MAX(id) maxId FROM Articles) m
limit 100
) r
join Articles a on a.id = r.randomId
order by r.id -- only need it for small tables. will slow down the query on big tables
limit 6
The best value for LIMIT in the subselect depends on percentage of gaps in your ids. 100 should be enough and fast.
Update
If you need to filter by category you can add a WHERE a.category = 'entertainment' clause before ORDER BY and LIMIT. But in that case you will need to ajust the number of generated random ids.
For example: If you have inserted 1M articles but 10% of them are deleted, then an average of 90 randomly generated ids do really exist. If now 10% of articles have category = 'entertainment', then an average of 9 random rows will match the condition. Average means - it might be 3 and might also be 16. So you need to generate more random ids to be sure, that you get at least 6 articles. With LIMIT 1000 in the subselect you will get an average of 90 random entertainment articles. This way you are very unlikely do get less than 6. So you need to know the statistics of your table in order to pick a good LIMIT.
Another issue with the WHERE clause, is that MySQL might reverse the join order to use an index for filtering. This might be faster for small number of generated random ids, but might be slower if the LIMIT in the subselect is huge. You can force the join order by using STRIGHT_JOIN instead of JOIN - But in my test with LIMIT 10000 it didn't make a
measurable difference.
If your condition is too selective (e.g. only 1% of articles have category='entertainment') a simple ORDER BY RAND() can be faster, because otherwise you would need to create too many random ids. But up to 10K rows matching your condition ORDER BY RAND() will be fast enough.

Finding a ranking from a rating field on MySQL

I have a MySQL table that looks like this:
id (int primary)
name (text)
rating (float)
I have a page showing rankings which looks like this:
$i = 0;
$q = mysql_query("SELECT * FROM teams ORDER BY rating DESC");
while($r = mysql_fetch_assoc($q)){
$i++;
print("$i: {$r['name']}<br>");
}
This shows teams in order of their rating, with a ranking. And it works.
Now, if I'm given the ID of a team, how do I find their ranking without running through the loop like this? A single MySQL query which returns the team's info + a numeric ranking indicating how far down the list they would be, if I had rendered the whole list.
Thanks!
To get the ranking you can do:
SELECT COUNT(*) as ranking
FROM teams t
WHERE t.rating >= (SELECT rating FROM teams WHERE id=$ID);
To get all the relevant info too, you can do:
SELECT t.*,COUNT(*) as rank
FROM teams t
JOIN teams t2 ON t.rating<=t2.rating
WHERE t.id=4;
This joins teams to itself joining on t.rating <= t2.rating, and so you get one row for every team that has a rating higher than or equal you.
The COUNT just counts how many teams have a rating higher than or equal to you.
Note that if there's a tie this will give you the lower rank. You can change the <= to a < if you want the highest.
You can also do it this way:
select * from (
select t.*, #rank := #rank + 1 as rank
from (select #rank := 0) as r, t
order by rating desc
) as t
where id = 20

SELECT * FROM table WHERE field IN (SELECT id FROM table ORDER BY field2)

I have 4 tables:
categories - id, position
subcategories - id, categories_id, position
sub_subcategories - id, subcategories_id, position
product - id, sub_subcategories_id, prod_pos
Now I'm doing tests to find out what's wrong with my query.
So i want to select sub_subcategories, and to get someting like that:
[[1,2,3,4,5,6], [1,2,3,4,5,6,7]], [[1,2,3,4,5,6], [1,2,3,4]]
Each [] means: big - categories, small - subcategory, and the numbers are position in sub_subcategories. I want the [] to order by their "position" field, so query:
SELECT id FROM sub_subcategories_id
WHERE subcategories_id IN (
SELECT id
FROM subcategories_id
WHERE categories_id IN (
SELECT id FROM categories
WHERE id = 'X' ORDER BY position)
ORDER BY position)
ORDER BY position
is somehow wrong, because I get:
1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,6,6,6,7
Dunno why - does last "ORDER BY position" destroy everything?
You need to apply all of your desired ordering in the outermost query - ORDERing within subqueries doesn't make any sense - the question "is this ID in <this list>?" has the same answer, no matter what order the list is in (indeed, more property, <this list> is a set, which has no order).
So you'll need to get all of the columns you need to order by in your outermost query.
Something like:
SELECT ssi.ID
from
sub_subcategories_id ssi
inner join
subcategories_id si
on
ssi.subcategories_id = si.id
inner join
categories c
on
si.categories_id = c.id
where
c.id = 'X'
order by
c.position,
si.position,
ssi.position
As it stands now, your query would never return a 'set' of numbers as is. If you ignore all the subselects, you're essentially doing:
SELECT id FROM sub_subcategories_id
ORDER BY position
which would only return one column: the sub_sub_categories_id. You'd be better off doing something like:
SELECT cat.id, subcat.id, subsubcat.id
FROM sub_sub_categories AS subsubcat
LEFT JOIN sub_categories AS subcat ON subcat.id = subsubcat.subcategories.id
LEFT JOIN categories AS cat ON cat.id = subcat.category_id
WHERE (cat.id = 'X')
ORDER BY cat.id, subcat.id, subsubcat.id
That'll return 3 columns ordered by the various IDs. If you don't need the individual sub_sub_categories values, and just want them as a single string value, you can mess around with GROUP_CONCAT() and do various bits of grouping:
SELECT cat.id, subcat.id, GROUP_CONCAT(subsubcat.id)
FROM ...
...
WHERE (cat.id = 'X')
GROUP BY cat.id, subcat.id, subsubcat.id
ORDER BY ...

Advanced SQL query. Top 12 from each category (MYSQL)

I have a MYSQL5 database and PHP 5. I need a query for a games websites index page that only selects the first 12 from each category of games. Here is what I have so far.
$db->query("SELECT * FROM `games` WHERE status = 'game_published' AND `featured` = '1' ORDER BY `category`");
The php code then groups games of the same category together and displays them. But yeah it doesn't limit the number of games from each category like I want.
Here is exactly what the structure of the table looks like: i49.tinypic.com/aysoll.png
Here is a blog post which sounds like what I am trying to do: http://www.e-nformation.net/content/view/title/MySQL+Top+N+in+each+group+(group+inner+limit) But I can't make sense of it.
Any help is appreciated.
How about this?
SELECT * FROM (
SELECT
games.*,
#rn := CASE WHEN #category=category THEN #rn + 1 ELSE 1 END AS rn,
#category := category
FROM games, (SELECT #rn := 0, #category := NULL) AS vars
WHERE status = 'game_published' AND featured = '1'
ORDER BY category
) AS T1
WHERE rn <= 12
you could use UNION, if we are not talking about million of types...
pseudoSQL:
(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)
UNION
(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)
UNION
(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)
If you have array of categories in your PHP/ASP, you can generate this union on the fly.
More:
http://dev.mysql.com/doc/refman/5.0/en/union.html
EDIT:
Here's probably most useful resource: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
Use it well ^^
There may be a more elegant solution, but you can just execute a query for each category. First get a list of categories:
SELECT DISTINCT(category) FROM `games`;
Then take each of the results and query for 12 rows:
SELECT * FROM games WHERE status = 'game_published'
AND `featured` = '1' AND `category` = $category LIMIT 12;
Of course you need to add some kind of ranking row (and order by it) to get the top 12.
Note: There may be a way to do this with a single query, but it escapes me at the moment.
To use the technique from the posts you mention, you need a way to order the games. They're using article date. Then they select the number of older articles for that company, and say there can't be more than three.
If your games table has an auto-increment column called id, you can select the top 10 games per category like:
SELECT *
FROM games g1
WHERE status = 'game_published'
AND featured = '1'
AND 10 >
(
SELECT COUNT(*)
FROM games g2
WHERE g2.status = 'game_published'
AND g2.featured = '1'
AND g1.category = g2.category
AND g2.id > g1.id
)
The where condition says that there can't be more than 10 rows with the same category and a higher ID.

Categories