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();
Related
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();
As first please be nice to me im a beginner with SQL language and english language.
So i have problem with this query.
I've created sqlfiddle.
My query look like it's working properly but I find it is not working
I would like to write a query that returns the product ID variant based on the parameters that will send
Correct query result would look like the following
PARAM = 6
id product_id productvariant_id attributevalue_id
1 3 1 6
2 3 2 6
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
PARAM = 6,9
id product_id productvariant_id attributevalue_id
3 3 3 6
4 3 3 9
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
What i really need is return productvariant_id which contains combination of inserted params, if I send only one attributevalue_id, i need to find productvariant which contain only ONE attributevalue_id. IF i send two params i finding combination of two.. not more or less
Unfortunately I do not have enough reputation for a comment, thus I have to write this as answer.
Could you clarify your goal?
Do you want to retrieve the productvariant_id of all datasets where the attributevalue_id matches your parameter?
SELECT productvariant_id
FROM productvariantattribute
WHERE attributevalue_id IN ($YOUR_PARAMETER_LIST);
Or do you want to retrieve the productvariant_id of all datasets where the productvariant_id has only these attributevalue_ids you specified as parameter?
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` IN ($YOUR_PARAMETER_LIST)
AND `productvariant_id` NOT IN (
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` NOT IN ($YOUR_PARAMETER_LIST)
)
I have to find a solution for my ranking scenario.
Say i have 100 users in database. And there is 5 subject in another table. In my project users have an option to rate 1 to 5 rating of any subjects. Means a user should have 5 different rating for 5 different subject. I need to shoe on the front page which is the most rated subject on the landing page.
I have a plan like create a table like
table name: user_subject_rate
Structure
id: user_id: subject1: subject2: subject3: subject4: subject5
1 1 3 1 2 5 4
When i create a table like this what happens if a new subject come to rate?
Can anybody suggest me a solution for this? I am using mysql as database.
I would advise you to structure your tables like below.
users
user_id | name
-----------------
1 Michael
2 Andrew
3 Annie
subjects
subject_id | name
----------------------
1 Maths
2 English
3 Physics
4 Chemistry
5 Biology
users_subjects_scores
user_id | subject_id | score
----------------------------
1 1 5
1 2 5
1 3 4
1 4 2
1 5 3
2 1 1
2 2 2
2 3 4
2 4 5
2 5 3
Then you can work out the total score of each subject using this query:
SELECT
name,
COALESCE(SUM(score), 0) AS total_score
FROM
subjects
LEFT JOIN
users_subjects_scores USING (subject_id)
GROUP BY
subject_id
ORDER BY
SUM(score) DESC
Then adding a new subject is as simple as adding a new row to the subjects table.
You can see an SQL fiddle here.
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
For some reason I feel this is a very easy task but I just can't get my head around it. I have two tables. Table A is list of companies called company and Table B is a list of users called users. The user table has a filed called 'access' which stores the ids of all the companies that a user is allowed to see.
companies
id name
-- ----
1 coca cola
2 samsung
3 apple
4 microsoft
users
id access name
-- ------ ----
1 1,3,4 brain owen
2 2,3 janet smith
2 1,2,4 peter pete
2 2,3,4 jane dow
My problem is how do I display the list of users who have access to a particular company? eg coca cola. Thank you.
id name
-- ----
1 coca cola
2 samsung
3 apple
4 microsoft
userId companyId
------ ---------
1 1
1 3
1 4
2 2
2 3
id name
-- ----
1 brain owen
2 janet smith
Two users can't have the same ID...
I think this will give you the user name of coca cola
select name from users where access in(1)