Table1
id name calculated_rating
1 xyz 2
2 abc 4.5
3 zzz 1
4 ddd 3
5 eee 2
Table2
id f_id rating
1 1 3
2 2 4
3 2 5
4 3 1
5 1 2
6 4 3
7 5 2
I have two tables one is Table1 and other is Table2
In table2 f_id that is foreign_key Table1 id is primary_key
Now Table2 has rating I want to added the calculate average rating in Table1 whenever Table2 increase rating it calculate average and update into Table1 calculated_rating field
How to achieve this in laravel
Whenever you add records to table2 like following you need to update current_rating value in table1.
Example,
id = 7
f_id = 5
rating = 2
You can create model for each table such as Table1 and Table2 using artisan command.
Now, Insert records like this:
$table2 = new Table2();
$table2->f_id = 5;
$table2->rating = 2;
$table2->save();
Now update new rating like this:
Table1::where('id',$table2->f_id)->update(['calculated_rating'=> Table2::where('f_id',$table2->f_id)->avg('rating')]);
Hope, this might help you.
Related
I got the following structure:
admin_id || country_id
---------------------------------
1 2
5 1
1 2
2 3
5 62
1 1
3 62
How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?
So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.
It should return this:
admin_id || country_id
------------------------
5 1
5 62
1 1
3 62
How can I do this in one sql query?
You can use a where clause for filtering on the admin_id or the country_id:
select t.*
from t
where t.admin_id = 5 or
t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
SELECT t1.*
FROM table t1
JOIN table t2 USING (country_id)
WHERE t2.admin_id = 5
fiddle
I've a table syntax like this,
- table1
id foreign_table_id
-----------------------
1 2
2 2
3 2
4 1
5 1
6 1
The other table is,
- table2
id value
------------
1 20
2 10
I want to get a summation of table1.foreign_table_id where the data will retrieve from table2.
Like this example should produce the result 90
Please provide any solution.
Use below query.
Select sum(table2.value) as total FROM table1 LEFT JOIN table2 ON table1.foreign_table_id = table2.id
It will give u below result.
Output::
total
-----
90
SELECT SUM(table2.value) AS summation FROM table1 LEFT JOIN table2
ON table2.id = table1.foreign_table_id
I'm working with PHP and MySql. I'm trying to find a way to select a number of movies from a mysql table, but apart from the movies table I have a watchlist table that stores a userID and the movieID of the movies he/she has added to his/her watchlist:
id userID movieID
=====================================
1 1 3
2 1 5
3 1 7
4 2 3
5 2 2
6 3 2
The movies table looks something like this
movieID title duration
=============================
1 tit1 34:43
2 tit2 35:43
3 tit3 24:43
4 tit4 34:13
5 tit5 11:43
6 tit6 22:43
7 tit7 33:43
The result I'm after is (for example for the user with ID 1):
movieID title duration added
=======================================
1 tit1 34:43 false
2 tit2 35:43 false
3 tit3 24:43 true
4 tit4 34:13 false
5 tit5 11:43 true
6 tit6 22:43 false
7 tit7 33:43 true
Is there a way to join both the movies and the watchlist table to produce the desired result?
Thanks.
You can get the required output by using a LEFT JOIN and then checking for a NULL in the join table.
SELECT m.*, IF(w.id IS NULL, 0, 1) AS added
FROM movies m
LEFT JOIN watchlist w ON (m.movieID = w.movieID AND w.userID = 1)
GROUP BY m.movieID
I have two table (table 1 and table 2) . I want to show all the rows from table1 by joining with table2 which have multiple rows with same table1 id (foreign key relation) and will sort the result by table2 priority column (order by desc).
Table1
Table2
Result will be
thanks in advance
Edit
Table1
id name
1 test1
2 test2
3 test5
4 test7
5 test9
6 test3
Table2
id table1_id event priority
1 2 abc 0
2 2 kbc 0
3 2 abc 2
4 2 kbc 1
5 4 fgg 2
6 4 dss 3
7 1 fgfg 2
8 5 fgfg 2
9 6 xcxc 1
10 6 fgfh 3
Result
id_table1 name event priority
4 test7 dss 3
6 test3 fgfh 3
2 test2 abc 2
1 test1 fgfg 2
5 test9 fgfg 2
3 test5 NULL NULL
In the question you mentioned you need to select the data where id from table1 is available more than once in the table2 which does not match with the result set you gave.
Considering the original requirement the following should do the trick
select
t2.table1_id as id_table1,
t1.name,
t2.priority,
t2.event
from table1 t1
join
(
select
p1.table1_id,
p1.event,
p2.priority
from table2 p1
join(
select
max(priority) as priority,
table1_id
from table2
group by table1_id having count(*) > 1
)p2
on p2.table1_id = p1.table1_id and p2.priority = p1.priority
)t2
on t1.id = t2.t1id
order by t2.priority desc
Here is a demo
The result will get the same event corresponding the max priority column
This will get the result set that you want. You mentioned that you only need the items table1 ids that reflects more than once but result query shows tableid1 "1" even though it is only present once:
SELECT DISTINCT t1.id,t1.name ,t2.event, t2.priority
FROM TABLE2 t2
right join
TABLE1 t1
on t1.id=t2.table1_id
order by t2.priority desc
Try this query:
SELECT t1.*,t2.priority FROM table1 t1
INNER JOIN table2 t2
ON t1.id=t2.id
ORDER BY t2.priority DESC
Primary key and foreign key should have the same name. The syntax should be
SELECT Table1.id_table1,Table1.name,Table2.event,Table.priority FROM
Table1 LEFT JOIN Table2 ON
Table1.id=Table2.id
ORDER BY Table2.priority DESC
Make the following changes in Table2:
Get rid of the first column or rename it
Rename second column(your foreign key) to "id".
I have two tables:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
Thanks
you can merge them like that with UNION .
try this:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
DEMO HERE
EDIT:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
DEmo with other columns
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1