How can I grab the top 10 most occurring values in mysql table column using laravel eloquent? - laravel-5.4

I want to grab the top 10 most occurring values in mysql table column using laravel 4.2.
For example, the top 10 occurring products and its counts, as in the below raw MySQL query:
SELECT `product_id`, COUNT(*)
FROM `product_details`
GROUP BY `product_id`
ORDER BY COUNT(*) DESC
LIMIT 10;

Try this syntax:
DB::table('product_details')
->select('product_id', DB::raw('COUNT(*) AS cnt'))
->groupBy('product_id')
->orderByRaw('COUNT(*) DESC')
->take(10)
->get();

Related

In PhpMyadmin Count rows issue with mysql

I added below query in phpmyadmin
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below output showing (0- 30 results) total 24000 records found
I tried same query in different format
SELECT count(u_id) As cnt,id,u_id
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below Query Executed Successfully.SHowing just 30 records , No Pagination dropdown.I can't click id and u_id field heading to sort..Someone please help what is the issue..I think both query should execute 24000 records..Please advise
PhpMyAdmin display default results as per it's settings which you can change from Number of rows : dropdown value in the interface.
If you need to display all your result, then you must add limit clause in your query to display all records
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
LIMIT 0, 999999
Otherwise, it will display 30 resuts (or whatever default PhpMyAdmin settings)
NOTE: To sort out the results, add Order by clause in your query then the query would look like following:
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
ORDER BY `id` DESC
LIMIT 0, 999999
Hope, it helps you.

sql to eloquent - nested select to apply group by

I would like to perform the following query in Eloquent.
SELECT * FROM (
select
`id`,
`category`,
`name`,
`slug`,
`value`,
`value_validation`,
`value_type`,
`created_at`
from
`configurations`
order by `created_at` desc
) as `a` GROUP BY a.slug;
Essentially, I need to store a history of all configurations in the DB. So will not be using the updated_at field, but rather creating a new row every time the configuration is updated. The old configuration will then no longer be used and the latest will always be applied.
slug is not unique. But my intention is for the group by statement to ensure that I get back a unique slug.
I want to be able to use all of Eloquent's goodness - and as such, would ideally want an eloquent object - not a query builder object.
Here's what I have so far, which generates the inner query. Now all I need to do is modify it to apply the outer query.
$configurations = $this->configuration
->orderBy('created_at', 'DESC')
->get();

MySQL - View's SELECT contains a subquery in the FROM clause

I am currently working the first time with mysql and Views I came into it quite well but when I execute this SQL-statement I ran into an error. I sadly couldn't find a solution yet. It says that the view contains a subquery but I can't see or realise any subquery in this statement.
I am refering to this post: Adding extra column to view, which is not present in table
The guy there is using similar code and for him it is working, no idea why it isn't in my example. I also looked through Stackoverflow and saw a load of the same errors but the subquery was quite obvious in the other topics.
CREATE OR REPLACE VIEW
`Worker_!view` AS
SELECT * FROM
(
SELECT `Working_!skill`,
'asdf_!electrical' charserver
FROM `asdf_!electrical`
WHERE 1 ORDER BY `id` DESC LIMIT 1
UNION
SELECT `Working_!skill`, 'fred_!electrical' charserver
FROM `fred_!electrical` WHERE 1 ORDER BY `id` DESC LIMIT 1
);
#1349 - View's SELECT contains a subquery in the FROM clause
That doesn't work in MySQL. Instead, you can do:
CREATE OR REPLACE VIEW `Worker_!view` AS
(SELECT `Working_!skill`, 'asdf_!electrical' charserver
from `asdf_!electrical`
WHERE 1 ORDER BY `id` DESC
LIMIT 1
)
UNION ALL
(SELECT `Working_!skill`, 'fred_!electrical' charserver
FROM `fred_!electrical`
WHERE 1
ORDER BY `id` DESC
LIMIT 1
);

mysql: SELECT WHERE id IN() and others

I'm using this query to select a set of records from a MySQL database:
SELECT *
FROM table
WHERE id IN(10,14,12,11,8,7,4)
AND actief='on'
ORDER BY FIELD(id,10,14,12,11,8,7,4)
This will give me the given ID's. In this case I will get a maximum of 7 records. But it can be less if for example ID '14' has active='off'.
But what I need is a set of 20 records where het list IN(10,14,12,11,8,7,4) must be in the result if they also meet the condition active='on'. If this returns 6 records, then I want the query to select another 14 records. Selecting highest ID first, must meet active='on' and may not already be in the result.
Can this be achieved by one SQL statement. Or should I first put the result of the mentionend query in an array and in a second array select the remaining records. And finaly put those also in the array?
You want to sort rather than filter the results. I think this is the query you want:
SELECT *
FROM table
ORDER BY (id IN(10,14,12,11,8,7,4) AND actief = 'on') desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
EDIT:
The final solution only wanted actief = 'on', so:
SELECT *
FROM table
WHERE actief = 'on'
ORDER BY (id IN (10,14,12,11,8,7,4)) desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;

get mysql results by highest count

I have a table with fields:
id, albumid, userid, keywords where keywords is varchar and can be more than one by delimteter eg : one,two,three
I want to get the top 10 results of the most popular keyword but not by the same user
I currently use this but not sure if its correct:
$tableName = $db->nameQuote('#__mytable');
$sql = "SELECT `id`,`albumid`,`userid`,`keywords`, COUNT(keywords) AS popular FROM ".$tableName." GROUP BY `userid` HAVING COUNT(*) > 1 ORDER BY popular DESC LIMIT ".$lim0.",".$lim;
$db->setQuery($sql);
Is the following code correct. Im not sure if im getting the keyword with most duplicate entries...
Try this query on for size, I just fudged around your groupings, and using DISTINCT userid as the counting metric
"SELECT `id`,`albumid`,`keywords`, COUNT(DISTINCT userid) AS popularity FROM ".$tableName." GROUP BY `keywords` HAVING popularity >= 1 ORDER BY popularity DESC LIMIT ".$lim0.",".$lim;
However, if your keywords column is comma delimited, this query will NOT work. This query operates on the fact that each record has a single keyword entry