Selecting an id with inner join - php

I'm trying to construct an user profile, so I'm showing all her likes from the database.
But I want to look if the user that have the active session has liked some of the user profile likes.
So, the table name is loves and the structure is:
photo_id (int)
nick (varchar)
date (timestamp)
photos table structure:
photo_id (int)
path (varchar)
title (varchar)
category (varchar)
nick (varchar)
date (timestamp)
This is how I'm traying to do the query:
SELECT photos.photo_id
FROM photos
INNER JOIN loves ON loves.nick = 'userProfileName'
WHERE loves.nick = 'userWithActiveSession'
AND photos.photo_id = loves.photo_id
ORDER BY loves.photo_id DESC
LIMIT 100
This query should return all photo ID's that the user with active session have liked with the liked photos from the profile requested user.
EXAMPLE
loves table:
nick photo_id
userProfile 26
userProfile 1000
userProfile 27
userProfile 520
userSession 26
userSession 680
userSession 1000
So the query should return only two photos_id (1000 and 26), because both users has liked the same photo_id.
Is there any way to modify this code to do what I want?

So you want all the photos owned by X (photos.nick = X) and liked by Y?
SELECT photos.photo_id FROM photos INNER JOIN loves
ON loves.photo_id = photos.photo_id
WHERE loves.nick = Y AND photos.nick = X
ORDER BY photos.photo_id DESC LIMIT 100
If you want photos liked by both X and Y then you need to join loves to itself, matching the photo_ids from the two copies of the table to each other, and conditioning that one table's nick matches X and the other's matches Y. (See comments)

you could get the photo_id without join like this:
SELECT photo_id
FROM loves
WHERE photo_id in (select photo_id from loves where nick = "userProfile" )
AND photo_id in (select photo_id from loves where nick = "userSession" )
GROUP BY photo_id
ORDER BY loves.photo_id DESC
LIMIT 100
DEMO HERE

That part looks weird:
INNER JOIN loves ON loves.nick = 'userProfileName'
shouldn't it be (assuming there is a nick field on photos table):
INNER JOIN loves ON loves.nick = photos.nick
or I didn't get something here?

Related

How to get the select the sum of a column JOINing with another table where elementxof table1 = elementx of table2?[special case]

I have two tables named videos and rating.
The first table
videos
uploader video_id
james ac0255
james ue2145
isabell qw2378
The second table:
rating
video_id score
ac0255 4
qw2378 2
ue2145 6
I want to store in variable x the sum of the score of all the videos uploaded by james.
Can anyone suggest an SQL query for it?
Try with this:
SELECT SUM(rating.score)
FROM videos
INNER JOIN rating
ON videos.uploader = rating.video_id
WHERE videos.uploader = 'james';
A simple join will do.
SELECT sum(r.score)
FROM videos v
JOIN rating r ON (v.video_id = r.video_id)
WHERE v.uploader = 'james';
In order to get the sum of score for James , you need to use JOINS and SUM function of mysql
SELECT SUM(Rating.score)
FROM videos as Videos
INNER JOIN rating AS Rating
ON Videos.uploader = Rating.video_id
WHERE Videos.uploader = 'james';

Inner join with 3 tables

I'm working with PHP and PDO, and I need to recolect information joining 3 tables:
photos
albums
album_photos
The table have the following structure:
photos:
photo_id (int)
path (varchar)
nick (varchar)
date (timestamp)
albums
album_id (int)
album_name (varchar)
nick (varchar)
date (timestamp)
album_photos
album_id (int)
photo_id (int)
nick (varchar)
date (timestamp)
So, I want to show all the albums with a max of 5 photos for each one, where the user nick is 'owner'.
To be shown as follows:
album_name_1:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
album_name_2:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
I only know that something like this can be made with INNER JOIN, but I can't found how I can make it with 3 tables.
There examples or other help that I can get to do this?
SELECT a.*, c.date
FROM Album a
INNER JOIN Album_Photo b
ON a.Album_ID = b.Album_ID
INNER JOIN Photo c
ON b.Photo_ID = c.Photo_ID
WHERE c.Nick = 'owner' AND
(
SELECT COUNT(*)
FROM album_photo d
WHERE b.album_id = d.album_id AND
d.nick = 'owner' AND
b.date >= d.date
) <= 2 // <<== change this value to 5
SQLFiddle Demo
I think this can be resolved using Stored Procedures. Using variables and loops, you can retrieve the records you desire. Getting only a maximum of 5 records from an album is quite challenging when you only use the basic SQL commands. Sometimes, you cannot derive the right records. I suggest you use Stored Proc. :)
Try like
"SELECT albums.*,photos.id,photos.path,photo_date as Pht_date
FROM albums
JOIN album_photos
ON album_photos.album_id = albums.album_id
JOIN photos
ON photos.photos_id = album_photos_id
"
But you cont give individual limit for photos where it involved in "JOIN" orelse you need to give individually like
$album_ids = "SELECT album_id FROM albums";
then for the photos you need to write query liks
"SELECT photos.* FROM photos
WHERE album_photos.album_id in (".$album_ids.") AND album_photos.nick = 'owner'
JOIN album_photos
ON album_photos.photo_id = photos.photos_id
LIMIT 0,10
"

How to find mysql difference between 2 tables

Pictures and Seen_pictures, a Picture from the Pictures table is displayed to a user, that Picture (it's ID in the table) is then moved to the Seen_Pictures, and a new picture from the Pictures table is shown to the user. I need a mysql scheme that will output the difference between the Pictures and Seen_pictures table, that way I know what pictures a user hasn't seen, and can output them.
I have this so far, but it only works for 1 user, I need it to account for many different users:
$result = mysqli_query(
$link,
"SELECT o_Pics.Pic.PicID
FROM o_Pics.Pic
LEFT JOIN o_SeenPics.Seen ON o_Pics.Pic.PicID=o_SeenPics.Seen.PicID
WHERE NOT o_Pics.Pic.ID='".$ID."' AND o_SeenPics.Seen.PicID IS NULL"
);
How about
SELECT p.p_id FROM Picture p WHERE p.p_id NOT IN
(SELECT s.p_id FROM Seen_Picture s WHERE s.u_id = "$user_id")
Picture
p_id(Primary Key) picture
Seen_Picture
id(Primary Key) u_id p_id
I think you can make some minor modifications to your original query to get what you want:
SELECT s.UserId, p.PicID
FROM o_Pics.Pic p LEFT JOIN
o_SeenPics.Seen s
ON p.PicID = s.PicID and
p.OwnerUserId != s.UserId
where s.PicId is null and p.OwnerUserId != s.UserId
This assumes that pic has a user id of the owner in it. It also returns the userid with the picture not seen.

Need help with PHP and MySQL…

I have a table of songs, some songs are album song, and some are singles... And I have a table of albums...
Songs table cols: Song_ID, Album_ID, Song_Name, Date_Released, Timestamp_Released, others...
If the Album_ID is [null], it means the song is a single
Albums table cols: Album_ID, Album_Name, Song_IDs, Date_Released, others...
Note 1: there is no Timestamp_Released in albums table
Note 2: Date_Released is only a day with no time i.e. "2011-06-16"
I'm currently using this query to display a table (in my html/php page) that each row is a single or a album (songs that are in an album are displayed all in one row as album)
SELECT
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name,
IF(Album_ID IS NULL,s.Date_Released,a.Date_Released) as datereleased,
s.Timestamp_Released
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1,2
ORDER BY 2 DESC,1
LIMIT 0,10;
The query above order the list of songs and albums according to date and give the albums the Date_Released and Timestamp_Released of the oldest song in the album...
So my question is how to give the album the Date_Released and Timestamp_Released of the newest song in it ?
Thank you :)
Instead of s.Date_Released, s.Timestamp_Released write MAX(s.Date_Released) as Newest_Date_Released, MAX(s.Timestamp_Released) as Newest_Timestamp_Releasd
UPDATE
SELECT
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name,
MAX(IF(Album_ID IS NULL,s.Date_Released,a.Date_Released)) as datereleased,
MAX(s.Timestamp_Released)
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 0,10;

php MySql QUERY for Friends relations Table help

EDIT by:lawrence.
I got the right query now
select *
from users, friends
where (users.id=friends.user_id1 and friends.user_id2=$profileID) or (users.id=friends.user_id2 and friends.user_id1=$profileID)
Question answered
I need some help joining results from my friends and users table
This is what my friends table look like
id user_id1 user_id2
1   | 2         | 3
1   | 2         | 4
1   | 2         | 5
1   | 6         | 2
Users table
id name
2 |  sarah
3 |  emma
4 |  lawrence
5 |  cynthia
6 |  suzie
I could easily just have two rows for each relation and do a simple query.
But i prefer having one row per relation,
So lets assume that we are watching page member.php?profile=2
and there is a list of friends, what does the query look like.
This works fine if i have two rows per relation but i dont want that....
SELECT * FROM friends, users WHERE friends.user_id1 = $profileID AND friends.user_id2 = users.id ORDER BY friends.id DESC LIMIT 16
Do you get me? something along like
SELECT * FROM friends,users WHERE friends.user_id1 = $profileID AND ALSO WHERE friends.user_id2 = $profileID AND THEN GET FROM users WHERE users.id = friends.user_id1 AND ALSO WHERE users.id = friends.user_id2
I hope I made myself clear
I'm not sure i understand your question but won't this do?
SELECT * FROM friends, users where friends.user_id1 = $profileID or friends.userid2 = $profileID and users.id = friends.user_id1 or users.id = friends.user_id2
You want a left join (using the LEFT JOIN operator), not a cartesian join (using the FROM table1, table2 syntax).
http://www.w3schools.com/sql/sql_join_left.asp
Tip: With your cross-reference table instead of having an id column you can create a compound key.

Categories