MySQL Query with JOIN Not executing correctly - php

I'm not getting any data, and I've checked my dad which should match up
I have two tables, likes and user_follow
I'm trying to tie the two tables by an id.
Table - Column
likes - idlikes, iduser, information
user_follow - iduser_follow, iduser_follower, iduser_following
$following = $dbh -> prepare("SELECT L.* FROM likes L JOIN user_follow F ON F.iduser_following = L.iduser WHERE F.iduser_follower = ?");
$following->execute(array($_SESSION['user_auth']));
while($row_following = $following->fetch(PDO::FETCH_ASSOC)){
$id_1 = $row_following['L.information']; // get id of user that i'm following
echo $id_1;
}
So if i'm following someone, i should be able to display information associated with whomever I'm following.
I don't get any errors, it just doesn't echo out anything?
Sample Data
user_follow
iduser_follow iduser_follower iduser_following
1 2 3
2 2 4
likles
idlikes iduser information
1 3 info1
2 3 info2
So, I should output info1 and info2, assuming that $_SESSION['user_auth'] = 2, correct?

SELECT b.*
FROM user_follow a
INNER JOIN likes b
ON a.iduser_following = b.iduser
WHERE a.iduser_follow = 'myuserID' AND
iduser_following = 'followingID'
and fetch value
$id_1 = $row_following['information'];

Related

How should be my mysql table structure?

This is what I want:
Users will send one or two values in my website and I will store them in two variables $genres1 and $genres2.
Like: If user sends, Action, then my code will show all movies with Action genres. If user sends Action+Crime, then my table will fetch all movies with Action+Crime.
Got it?
My current table structure is one to many relationship, like this
tmdb_id movie_title
-----------------------------
1 Iron man
2 Logan
3 Batman
4 The hangover
tmdb_id genres
-----------------------------
1 Action
1 Crime
2 Drama
2 Action
3 Crime
3 Action
4 Comedy
4 Drama
But the problem here is, I can't achieve what I explained above with this.
2nd option: I make a single table like this:
movie_tile genres1 genres2 genres3 genres4
----------------------------------------------------
Logan Action Crime Drama Null
Iron man Action Crime Null Null
And I can do what, I want with this single line:
SELECT * FROM movies WHERE (genres1='$genres1' or genres2='$genres1' orgenres1='$genres3' or genres3='$genres1')
Any other option?
use a table width genres
and use an other table connecting the movie to any genre
-----
movieid title
-----
1 Logan
2 Smurf
-----
-----
genreid genre
-----
1 animated
2 blue people
-----
-----
movieid genreid
-----
1 1
2 1
2 2
-----
that way you won't be limited to 4 genres per movie
now I read your question better.
That's what you do, but you put left out the genre-table.
The 2nd option is bad, as you limit yourself to only 4 categories
Is this connected to PHP? I think is easiest to solve this further by a join query, sorted by movie and a loop in PHP
you want all movies where (by user request) the genres are both Crime And Action?
SELECT mg.movieid, count(1), m.title
FROM movies_genres mg
JOIN movies m ON m.movieid mg.movieid
WHERE mg.genreid = 1 OR mg.genreid =3
group by mg.movieid, m.title
HAVING COUNT(1) = 2
edit: see other genres as well
SELECT movies.movieid,movies.title, genres.genre
FROM movies
JOIN movie_genre mg ON mg.movieid = movies.movieid
JOIN genres on genres.genreid = mg.genreid
WHERE movie.movieid IN (
SELECT mg.movieid
FROM movies_genres mg
WHERE mg.genreid = 1 OR mg.genreid =3
GROUP BY mg.movieid
HAVING COUNT(1) = 2
)
forgot to mention: count = 2, means you gave 2 genreid's to find. This could also be 1, 3 or 25
select distinct a.tmdb_id, a.movie_tittle
from movie_tittle a inner join genre_tittle b
on a.tmdb_id = b.tmdb_id
where b.genres in ('Action', 'Crime')
Based on your comment, try this :
SELECT
a.tmdb_id, a.movie_tittle
FROM
movie_tittle a inner join genre_tittle b
ON
a.tmdb_id = b.tmdb_id
WHERE
b.genres in ('Action', 'Crime')
GROUP BY
a.tmdb_id, a.movie_tittle
HAVING
count(a.tmdb_id) = 2
tmdb_id and genres in table genre_tittle should not duplicated. Make it as primary key.
But the problem here is, I can't achieve what I explained above with [the first two tables]
Yes, you can. Assuming the two tables are called movies and movie_genres, you can select the movies which have both tags using:
SELECT movie_title FROM movies
JOIN movie_genres genre1 USING (tmdb_id)
JOIN movie_genres genre2 USING (tmdb_id)
WHERE genre1.genres = 'Action'
AND genre2.genres = 'Crime'
See it for yourself here.
try something like this :
tableA
Movie_ID Movie_title
1 Iron man
2 Logan
3 Batman
4 The hangover
tableB
Genre_ID Genre_title
1 Action
2 Crime
3 Drama
4 Comedy
tableC
ID Movie_ID Genre_ID
1 1 1
2 1 2
3 2 2
4 2 3
query :
Select A.Movie_title,B.Genre_title
from tableC C
inner join tableA A on A.Movie_ID = C.Movie_ID
inner join tableB B on B.Genre_ID = C.Genre_ID
where
C.Genre_ID in (IFNULL(val1,0),IFNULL(val2,0))
you should make a relational table to solve you issues like so
movie table
movie_id movie_name genre_id
1 alien 2
2 logan 1
3 ps i love you 4
4 click 3
then you will need a genre table
genre table
genre_id genre_type
1 action
2 sci fi
3 comedy
4 romance
then your select would link the to tables
function get_movies_by_genre($genre_id) {
global $MySQLiConnect;
$query = '
SELECT *
FROM movies m
INNER JOIN genre g ON (g.genre_id = m.genre_id)
WHERE g.genre_id = ?
';
$stmt = $DBConnect->stmt_init();
if ($stmt->prepare($query)) {
$stmt->bind_param("i", $genre_id);
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
}
return $rows;
}
or
function get_movies_by_genre($genre_id) {
global $MySQLiConnect;
$query = '
SELECT *
FROM movies m
INNER JOIN genre g ON (g.genre_id = m.genre_id)
WHERE g.genre_name = ?
';
$stmt = $DBConnect->stmt_init();
if ($stmt->prepare($query)) {
$stmt->bind_param("i", $genre_id);
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
}
return $rows;
}
This is the base function to get you all information from the movie table depending on which genre id you send to it.
as for multiple ids you can then run the function through a foreach loop for as many genre_ids as you need and then display them as you need.
I hope this helps.

SQL joining other table with condition

I have this query:
SELECT *
FROM `classes`
JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class`
And I need to add condition for selecting just classes, in which are not currently logged student (user ID is not in classes_students connected with class id) and also count how many students are in that class.
Table structure:
classes: id, name, etc
classes_students: class_id, user_id, etc
Table data:
classes:
1 | test
2 | test2
3 | test3
classes_students:
1 | 1
1 | 2
2 | 3
3 | 4
3 | 5
Expected output if im user with id 1:
classes names (with number of students in):
2 (1 student)
3 (2 students)
All this in one query. It is possible? If yes, how?
Select classid, count(*)
from class
left join student on student.classid = class.classid
group by classid
Glad help for you
Try this query:
$user_id = 1; // current user_id
$query = "SELECT `classes`.`id`, `classes`.`name`, COUNT(*) as students FROM `classes`
JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class_id`
AND `classes_students`.`user_id` != $user_id
GROUP BY `classes_students`.`class_id`
";
I figured it out! :)
Thank you guys for ur help.
$user_id = 1; // current user_id
$query = "SELECT `classes`.`id`, `classes`.`name`, COUNT(*) as students FROM `classes`
LEFT JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class_id`
WHERE `classes`.`id` NOT IN (SELECT `class_id` FROM `classes_students` WHERE `user_id`='.$user_id.')
GROUP BY `classes_students`.`class_id`
";

Analyze table for mutual likes between users

In my database I have a table (likes) that lists items that users have liked.
When visiting a users profile I need to determine how many of those "likes" we have in common.
What's the best way to go about writing a query that will display the mutual likes between users?
likes table
id | user | item | activated
-----------------------------------
1 | 3 | 14 | 1
2 | 4 | 14 | 1
I need to return 14 in this example.
Suppose should be something like this:
select userViewer.item, items.itemName
from likes userViewer,
likes userProfile,
items
where userProfile.user = $profileUserId
and userViewer.user = $userViewer
and userProfile.item = userViewer.item
and items.item = userViewer.item
where $profileUserId - userId of the profile user and $userViewer - userId of current user
Same query on "join on" form with "items" table, as example:
select userViewer.item, items.itemName
from likes userViewer inner join likes userProfile
on userProfile.user = $profileUserId
and userViewer.user = $userViewer
and userProfile.item = userViewer.item
inner join items
on userViewer.item = items.item
I wants to know why you have not used matching caondition in where clause
SELECT
userViewer.item, items.itemName
FROM
likes AS userViewer
INNER JOIN
likes as userProfile
ON
userProfile.item = userViewer.item
INNER JOIN items
ON
userViewer.item = items.item
WHERE
userViewer.user = $userViewer
AND
userProfile.user = $profileUserId

SQL query for fetching friend list

I need help with SQL query.
I have two tables. One is users and other one is userfriends
users table:
aid email firstname
1 example#mail.com example
2 example2#mail.com example2
3 example3#mail.com example3
4 example4#mail.com example4
userfriends tables:
reqid email friendemail status
1 example1#mail.com example2#mail.com 1 (example1 is frnds with example2)
2 example2#mail.com example4#mail.com 2 (example2 request pending)
3 example3#mail.com example1#mail.com 1 (example1 is frnds with example3)
4 example4#mail.com example1#mail.com 1 (example1 is frnds with example4)
So when status is 2 the add request is pending and at status 1 they are friends. What i want is that i want to retrieve the complete friendlist for user example1. I want to pull out names from users table for corresponding output from previous query to display as friendlist.
I think you guys are missing the fact that the searched for email could be in either column:
select u.firstname
from userfriends f, users u where
u.email='email#domain.com' and f.status=1 and
(u.email = f.email and f.friendsemail='email#domain.com')
or
(u.email = f.friendsemail and f.email='email#domain.com')
select distinct friendemail
from userfriends f
inner join users u on u.email = f.email
where f.status = 1
and u.firstname = 'example'
You need to JOIN both tables:
SELECT users.firstname
FROM userfriends UF
JOIN users U ON (U.email=UF.friendemail)
WHERE UF.email='your user'

mysql retrieving newest entry between set integers

Okay the title may not justify what I'm trying to do here but dont know my problem well enough to explain it perfectly.
What i'm trying to do:
I need to retrieve rows from a MySQL data base where response = 8 or 9 but display the newest entry if there are multiple matches. Let say I have 6 rows and 1 rows have response = 8 and 2 row where response = 9 I want to retrieve the newest entry based on row id.
example:
row1 id=12 user_id = 15 response = 8
row2 id=11 user_id = 15 response = 9
row3 id=10 user_id = 15 response = 1
row4 id=09 user_id = 15 response = 9
row5 id=08 user_id = 15 response = 4
row6 id=07 user_id = 15 response = 5
I want to retrieve row1 since it's the newest entry where response = 8 or 9
I tried but failed:
SELECT a.id, MAX(a.response) FROM user a WHERE a.user_id = $userID AND a.response IN (8,9) GROUP BY a.user_id
Thanks in advanced.
SELECT u.id, u.user_id, u.response
FROM user u
WHERE u.id in
(
SELECT max(id) FROM user WHERE user_id = $userID and response IN (8,9)
) ;
ID USER_ID RESPONSE
12 15 8
SELECT a.id, MAX(a.response) -- Your columns
FROM user a -- Table
WHERE a.user_id = $userID -- your ESCAPED parameters (see mysql prepared stmts)
AND a.response IN (8,9)
GROUP BY a.response
ORDER BY a.id DESC -- the greater the ID, the 'newer'
LIMIT 1 -- just get one record
Assuming I have understood your question and you want the newest row (i.e. highest value in id column) for the specified user but only where the response is 8 or 9, try
SELECT id
FROM user
WHERE user_id = $userID
AND response IN (8,9)
ORDER BY id DESC

Categories