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
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
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.
I want to show top 5 users on logged user dashboard based on following criteria:
users are opting following attributes in their profile:
Relationship preferences,
Hobbies,
Interests,
Language etc
More the preferences will match, highest priority to user's profile will be assigned
table_users
userid ufname ulname gender
--------------------------------
1 test1 Test2 M
2 testF TestF F
3 testF1 TestF1 F
4 testF2 TestF2 F
5 testF5 TestF2 F
table_preferences
preference_id user_preferences
--------------------------------
1 Cooking
2 Gardening
3 Smoking
4 Single
5 widow
6 traveling
table_user_preferences
userid user_preference_id
--------------------------------
1 1
1 2
1 3
1 4
2 3
2 4
3 2
3 3
3 6
4 1
5 1
5 2
5 3
5 4
5 6
Now, suppose user one is logged in, on his dashboard other registered users (with opposite i.e. gender female) will get display whose highest attributes are matching. As we can see, user five 5 matches most of the attributes so it will be display on top of list and rest will be get display accordingly as follows:
Result required:
User_id ufname
5 testF5
3 testF1
2 testF
4 testF2
My Query is I am thinking to use relevance search. Will it be suitable. Anyone can help me any suggestions.
Simple use MYSQL JOIN and SUBQUERY
1) Use subquery to get the preference of logged in user.
2) Then join table_user_preferences with table_users with ON tu.userid = tup.userid and AND tu.gender !=$current_user_gender and IN( ) condition to match the prefrence with logged in user preference.
3) Finally group the user and apply order by count of match preference
select tu.*,tup.userid,count(1) as total_match
from table_user_preferences as tup
join table_users as tu
ON tu.userid =tup.userid
where tup.user_preference_id
IN (select up.user_preference_id from table_user_preferences as up where up.userid=$current_userid)
AND tu.gender !=$current_user_gender
group by tup.userid order by total_match desc limit 5
i am facing a very weird problem.
Basically i need to extract a set of rows from a table where every row is the "oldest" in its own group.
The table is structured as follow:
id, integer
domain_id, integer
value, integer
created_at, datetime
My query is
SELECT * FROM domains_urls GROUP BY domain_id HAVING created_at = MAX(created_at)
If i am not wrong, it should group the rows by domain_id and extract the one that it matches created_at = MAX(created_at).
The point is that it does not work as expected!
The table content is
id domain_id value created_at
1 2 1 2014-05-25 10:30:13
2 1 3 2014-05-25 19:30:13
3 2 2 2014-05-25 11:30:13
4 2 7 2014-05-25 15:30:13
5 2 4 2014-05-25 12:30:13
6 2 5 2014-05-25 13:30:13
I should get two rows:
id domain_id value created_at
2 1 3 2014-05-25 19:30:13
4 2 7 2014-05-25 15:30:13
Instead i get only
id domain_id value created_at
4 2 7 2014-05-25 15:30:13
I'm using MySQL 5.5 on Windows 7
I need to use an HAVING+GROUP BY or a DISTINCT+ORDER BY (not tested).
Thanks!
EDIT:
Because i am a dumb (i should avoid working on sunday), the value returned by MAX is related to the table and not to the group!
You can accomplish this by joining the table back to itself using the max(created_at):
select du.*
from domains_urls du
join (select domain_id, max(created_at) maxcreated_at
from domains_urls
group by domain_id
) du2 on du.domain_id = du2.domain_id
and du.created_at = du2.maxcreated_at
SQL Fiddle Demo
Try something like this
Select * from urls as u group by u.id having u.created >= any (select created from urls u2 where u.id == u2.id)
i have not tested this, just wrote that from the top of my head
I have this table
conncections
id_user connectedto
1 4
2 4
3 1
1 5
i would like to do a SELECT that gives me back all the numbers that have a relation with the number 1 and the number 1 itself. Therefore 1 4 3 5 as result.
Something like
SELECT DISTINCT id_user as related
FROM connections
WHERE connectedto = 1
UNION
SELECT DISTINCT connectedto as related
FROM connections
WHERE id_user = 1
With UNION duplicate are removed