How to retrieve and display 3 highest value in Laravel? - php

id
names
voted
1
John
100
2
Mike
25
3
Kathy
200
4
William
186
5
Jasmine
60
6
Sui
57
7
Chris
43
8
Stewie
103
9
Steve
150
10
Jennifer
97
I was able to retrieve the highest value in my table and decided to try retrieving and displaying the 3 highest value from my table but I don't know how and can't seem to find a tutorial or similar codes to what I'm trying to do. What should I do?
My code
public function votesList(){
$data=votes::all()->max('voted')->get();
return view('list',compact('data'));
}
The result I want
id
names
voted
3
Kathy
200
4
William
186
9
Steve
150

$data=votes::orderby('voted', 'desc')->limit(3)->get();

Related

recursively get all child categories from a parent id

I'm having trouble making a solution with this problem: my database looks like this.
ID parent_cat_id child_cat_id
1 27 10
2 15 20
3 80 90
4 90 88
5 60 30
6 88 40
7 15 60
8 30 40
9 27 95
10 40 30
The context is the user selects a category for product search, I need to get that category IDs and all of it's children which can go 4 levels deep.
The database is similar to this post:
Check if column value exists in another column in SQL
However the OP only needs 1 match for me it needs to be recursive. I'm still trying out several solutions but I'm at a loss. Would love some suggestions.
Thanks in advance.

Retrieve data from a grouped query in Laravel

Suppose I have this set of data:
id name amt
1 john 5
1 john 13
1 john 6
2 doe 3
2 doe 4
3 sue 5
3 sue 10
I want to extract a data that would look like this
name amt
john 5
13
6
doe 3
4
sue 5
10
How do i generate this using Laravel Eloquent query?
You can write your custom queries like as
DB::table('table_name')
->select('name',DB::raw('group_concat(amt)'))
->groupBy('name')
->get();

MySQL QUERY - loop through table and perform calculations with conditions (for PHP)

I'm working on one project and now I'm in blind spot, trying to resolve this..
I have MySQL table with name x_world with some columns. Here is an example:
internalid name population date
1 John 100 2014-05-01
2 Tommy 250 2014-05-01
3 Paul 200 2014-05-01
4 Holly 700 2014-05-01
5 John 100 2014-05-02
6 Tommy 240 2014-05-02
7 Paul 201 2014-05-02
8 Holly 680 2014-05-02
9 John 100 2014-05-03
10 Tommy 230 2014-05-03
11 Paul 202 2014-05-03
12 Holly 799 2014-05-03
What i need, is get this result:
name population (+-1d) (+-3d)
John 100 0 0
Tommy 230 -10 -20
Paul 202 +1 +2
Here is explanation about what i mean and need:
Max allowed population change (grow) is 2 (including 2). If population grows over 2, dont display this row in result.
If population is only descending (no matter how much), it's okay, display this row.
Combination of both above conditions: if population descends one day (no matter how much), and then grows (no more then 2) - it's okay too, display this row.
If population does not change during these 3 days, it's okay, display this row.
In addition:
The value of population displayed in result equals to the last known value (latest date).
The two values (+-1d) and (+-3d) are calculated.
I'm beginner in PHP and MySQL. I'm familiar to very basics of both, however, the MySQL part looks insane for me :( I appreciate every piece of help. And finally, sorry for my bad english. I did my best :)
SELECT cur.name, cur.population,
cur.population-d1.population AS `+-1d`, cur.population-d3.population AS `+-3d`
FROM (
SELECT name, MAX(date) AS curdate
FROM x_world
GROUP BY name) AS maxes
JOIN x_world AS cur ON cur.name = maxes.name AND cur.date = maxes.curdate
JOIN x_world AS d1 ON d1.name = maxes.name AND d1.date = DATE_SUB(maxes.curdate, INTERVAL 1 DAY)
JOIN x_world AS d3 ON d3.name = maxes.name AND d3.date = DATE_SUB(maxes.curdate, INTERVAL 2 DAY)
HAVING `+-1d` <= 2 AND `+-3d` <= 2
DEMO
Note that although you call it +-3d, there's actually only 2 days between 2014-05-01 and 2014-05-03. I've kept your naming, but use INTERVAL 2 DAY in the calculation.

Display each row one by one from each category one by one using php mysql

i have the following table structure: basically it is grouping by agency_id.
id agency_id price type
1 1001 10000 A
2 1002 13000 B
3 1001 16000 C
4 1003 11000 A
5 1002 12000 C
6 1003 9000 D
7 1001 15000 A
8 1002 12000 A
i want to display it as following
id agency_id price
1 1001 10000
2 1002 13000
4 1003 11000
3 1001 16000
5 1002 12000
6 1003 11000
i am using the following code, but its showing all the properties from 1 agency, then 2nd agency and so on.
ORDER BY
CASE properties.agency_id
WHEN 1001 THEN 1
WHEN 1002 THEN 2
WHEN 1003 THEN 3
END
but its not giving me the desire results. any help will be apriciated
Regards,
Use a solution like the one in Ranking by Group in MySQL to add a rank column that increments within each group, and put that in a subquery. Then do:
SELECT *
FROM (subquery) AS x
ORDER by rank, agency_id

Count amount of rows in database with the same value in two columns - Use amount of top 5 rows to set width of 5 different divs

id company_id company_name size_of_company employee
-------------------------------------------------------------------
1 1 Comp 1 Big John
2 1 Comp 1 Big Ann
3 1 Comp 1 Big Peter
4 2 Comp 2 Big Lisa
5 2 Comp 2 Big Steve
6 3 Comp 3 Big Mike
7 3 Comp 3 Big Anna
8 3 Comp 3 Big Jon
9 3 Comp 3 Big Nick
10 3 Comp 3 Big May
11 4 Comp 4 Big Lee
12 4 Comp 4 Big James
13 4 Comp 4 Big Jess
14 4 Comp 4 Big Carrie
15 5 Comp 5 Big Luke
16 5 Comp 5 Big Brad
17 5 Comp 5 Big Joan
18 5 Comp 5 Big Ruth
19 5 Comp 5 Big Joel
20 6 Comp 6 Big Paul
This is probably an easy question, but my skills are quite low when it comes to MySQL and PHP. I have tried different loops with no result at all. Any help would be much appreciated.
What I want to do is this:
How do I count the amount of rows with the same company_id and size_of_company and then collect the amount of rows for the top 5 along with the names of the top 5 companies?
The amount of rows will be used in a div as width value for the div. The div shall be printed 5 times with different width value. The more rows the bigger div.
There will also be values of Medium and Small in size_of_company. But a company_name can only have one size_of_company. These two other size_of_company shall also be printed on the page, under their own category, Medium or Small.
If I understand the question correctly, the following would count the number of rows with the same company_id, company_name and size_of_company (using GROUP BY). Ordering and then limiting the number of results gives you the top 5.
SELECT
t.company_id,
t.company_name,
COUNT(*)
FROM
tble t
GROUP BY
t.company_id, t.company_name, t.size_of_company
ORDER BY
COUNT(*) DESC
LIMIT 5

Categories