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.
Related
I have two tables.
One table has everything I need including a cardId(PK)
The other name is a user type table. This table stores the userId and the cardId(FK).
SELECT ci.cardId, ci.year, ci.name, ci.number
FROM USERCARDS uc
INNER JOIN CARDINDEX ci ON uc.cardId = ci.cardId
WHERE uc.userId = 'USER_ID'
So for this query, it will display the cardId, year, name, and number from the CARDINDEX. It will only display the cards that the user has saved in USERCARDS.
I want to do the opposite. If a user is looking at, lets just say, 5 cards, but the CARDINDEX has 50 cards, this query will display the information for the five cards. However, for a new query, I would want to show the remaining 45 cards. Basically, they cant add a card they already are following.
I tried to have uc.cardId != ci.cardId but that didn't work. Im kind of lost.
You can phrase this as a LEFT JOIN:
SELECT ci.cardId, ci.year, ci.name, ci.number
FROM CARDINDEX ci LEFT JOIN
USERCARDS uc
ON uc.cardId = ci.cardId AND
uc.userId = 'USER_ID'
WHERE uc.cardID IS NULL;
Alternatively, you could write this using `NOT EXISTS:
SELECT ci.cardId, ci.year, ci.name, ci.number
FROM CARDINDEX ci
WHERE NOT EXISTS (SELECT 1
FROM USERCARDS uc
WHERE uc.cardId = ci.cardId AND
uc.userId = 'USER_ID'
);
I have two tables, one for registered users and one to store votes.
We are logging in with registrants.id and registrants.zipcode. Once they vote their votes are inserted into the votes table, along with their Registration ID.
Im trying to right a select statement that returns a record that will select all the records for Matched ID and Zipcode, but the ID is not in the Votes.voter column. i have tried all kinds of variations of all the joins i can think of. is it something simple i am missing.
SELECT * FROM registrants
LEFT JOIN votes on registrants.id = votes.voter
WHERE registrants.id = 1 AND registrants.zipcode = 46706 and votes.voter <> 1
Perhaps a not exists query:
select * from registrants
where registrants.zipcode = '46706'
and not exists (select 1 from votes where registrants.id = votes.voter)
I have two tables, one of them is for friends of a person and another one is for channel member.
I want to check if one of my friends is one of the channel members. If so don't show his name, else show him.
$get_friend = "select * from friends where user_1_id='$user_id' AND friends_status=1";
$run_friend = mysqli_query($conn,$get_friend);
$select_members = "SELECT user_id from channel_members where channel_id='$channel_id'";
$run_members = mysqli_query($conn,$select_members);
Try this query :
select * from friends where user_1_id='$user_id' AND friends_status=1 AND user_2_id NOT IN(SELECT user_id from channel_members where channel_id='$channel_id')
This will select all your friends from friends table which are not a member of channel '$channel_id'
SELECT
friends.*
FROM
friends
LEFT JOIN channel_members
ON channel_members.channel_id = '$channel_id'
AND channel_members.user_id = friends.user_2_id
WHERE
user_1_id = '$user_id'
AND friends_status = 1
AND channel_members.channel_id IS NULL
You can use this query.
I added an excluding LEFT JOIN.
It will try to join the members of the channel who are your friends to the result, but by specifying channel_members.channel_id IS NULL in the where clause you are filtering those datasets out which really have a member in the channel - leaving you with only the friends who are not in the channel.
I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.
Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.
Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
3--------400.00------April
1--------200.00------April
1--------100.00------April
Example desired output:
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?
Query:
SELECT
properties.*,
leases.*,
users.userId, users.primaryPhone,
CONCAT(users.userFirstName,' ',users.userLastName) AS user,
admins.adminName, payments.*
FROM
properties
LEFT JOIN leases ON properties.propertyId = leases.propertyId
LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
LEFT JOIN admins ON assigned.adminId = admins.adminId
LEFT JOIN users ON properties.propertyId = users.propertyId
LEFT JOIN payments ON properties.propertyId = payments.propertyId
WHERE
payments.rentMonth = '$currentMonth' AND
payments.rentYear = '$currentYear'
Edit: Please excuse my formatting, this is my first post.
Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)
I suspect this is what you want:
SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
FROM yourTable
WHERE rentMonth = "April"
GROUP BY userID
ORDER BY maxRentPaid
I am currently using this query to select a media record in my PHP application and get the associated user data (if available):
SELECT media.*,
user.*
FROM media_table AS media
LEFT JOIN user_table AS user ON
(user.user_id = media.user_id)
WHERE media.media_id = {$mediaId}
What I'm interested in doing is selecting media.* for the next available media_table record and the previous media_table record. I could do this based on the media_id itself (media_table.media_id is auto increment) or there are other columns available such as media_date (this may be more accurate - it's an INT column in epoch time format).
And I would like to do this (if possible) via a join so I don't have to run multiple queries.
It reminds me of this answer: https://stackoverflow.com/a/1446831/1593325
It gave me some idea, but I couldn't get it to work (unless I ran separate queries).
Any help is much appreciated. Thank you so much for your time.
If you wish to get all the 3 rows at the same time, you can modify your WHERE clause to accommodate your requirements. The modified query would be :
SELECT media.*,
user.*
FROM media_table AS media
LEFT JOIN user_table AS user ON
(user.user_id = media.user_id)
WHERE (media.media_id = {$mediaId}
or media.media_id = ({$mediaId}-1)
or media.media_id = ({$mediaId}+1))
Update :
In that case you can use the query as
SELECT media.*,
user.*
FROM media_table AS media
LEFT JOIN user_table AS user ON
(user.user_id = media.user_id)
WHERE (media.media_id = {$mediaId}
or media.media_id = (SELECT MIN(media_id ) FROM media_table
where media_id > {$mediaId})
or media.media_id = (SELECT MAX(media_id ) FROM media_table
where media_id < {$mediaId})
)