I have a crossreference table owner_meeting_crossref with a column (owner_id) for owner IDs and column (meeting_id) for meeting IDs that the owner has access to.
My php script is sent an array of meetings, and I know the current user ID is $current_user_id. How can I efficiently check that the user (owner) has access to the meeting IDs sent in?
One option would be to fetch the meetings from the list that the user has access to, then calculate the difference.
$meetingStr = implode(',', array_map('intval', $meetings));
$accessibleMeetingQuery = $db->prepare("
SELECT meeting_id
FROM owner_meeting_crossref
WHERE owner_id=:uid AND meeting_id IN ($meetingStr)
");
$accessibleMeetingQuery->execute(array(':uid' => $current_user_id));
$accessibleMeetings= $accessibleMeetingQuery->fetchAll(PDO::FETCH_COLUMN);
$inaccessibleMeetings = array_diff($meetings, $accessibleMeetings);
Another would be to do it all from SQL
SELECT m.id
FROM meetings AS m
LEFT JOIN owner_meeting_crossref AS om
ON m.id=om.meeting_id AND om.owner_id=:uid
WHERE m.id IN ($meetingStr)
AND om.meeting_id IS NULL
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.
This is my table in which ID is table's primary key and SUPER_ID is used to manage hierarchy for example here, A is super user of B and B is super user of C while A is super-super user of C............
ID AND SUPER_ID MAY NOT NECESSARY BE SEQUENTIAL
Now question is when A is logged in, He can see details of his own and of Both B and C While B is logged in He can see details of His own and C only. And C can see his own details only.
I have written this query:
<?php
$sql="SELECT * from TABLE
WHERE
ID =:loginId OR
ID IN
(
SELECT ID FROM TABLE
WHERE SUPER_ID =:SuperId
)";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':logedIn' => $_SESSION['sess_login_id'] , ':SuperId' => $_SESSION['sess_login_id']) );
?>
This query gives me results of A and B when i logged in as User A.
What query should I write so that I can get results of A , B and C when I logged in as User A?
Because A is a super user of B and super-super user of C. Please help. Thank You.
If you have only three hierarchy levels, you can do it like this:
SELECT DISTINCT u.*
FROM user loggedin
LEFT JOIN user children ON children.SUPR_ID = loggedin.ID
LEFT JOIN user grandchildren ON grandchildren.SUPR_ID = children.ID
JOIN user u ON u.ID IN (loggedin.ID, children.ID, grandchildren.ID)
WHERE loggedin.ID = :loginId
http://sqlfiddle.com/#!9/3d93c/7
http://sqlfiddle.com/#!9/ad6a88/2/0
First get SUPR_ID from the user, then check which SUPR_ID is bigger or same.
SELECT * FROM `TABLE` WHERE SUPR_ID >= (SELECT SUPR_ID FROM `TABLE` WHERE ID=:loginId)
I'm having a bit of trouble figuring out an SQL statement that I need to make.
I have 2 tables
Friends
===================================
friendfromid | friendtoid | request
===================================
Users
=================
userid | username
=================
I need to get the userid and username of each user that is my friend.
My id is either in the friendfromid or friendtoid depending on if I am requested the user or the user requested me.
So basically what needs to happen is the script needs to look at the friends table, get all the rows where my id is either friendfromid or friendtoid check that the request field is set to 1, take all the rows that fit that match then get the ids and usernames of each friendfromid or friendtoid which isn't mine.
For instance, if my id was 8 and the friends id was 9, let's say they requested me their id would be in the friendfromid field and my id would be in the friendtoid field, that means the script would take their id (9) and match it to that user in the users table.
This will give friends list with friend id and friend name:
SELECT u.userid , u.username
FROM Friends f
JOIN Users u
ON (( f.friendfromid ={the ID} AND friendtoid=u.userid)
OR ( f.friendtoid = {the ID} AND friendfromid=u.userid))
WHERE f.request = 1
Put {the ID}= user id whose friend list required.
Should be something like this:
SELECT *
FROM users
JOIN friends
ON friendfromid != userid
OR friendtoid != userid
WHERE request = 1
AND userid = {the ID}
Can't achieve in 1 query.
To get friendtoid with "my user ID" in friendfromid field:
SELECT b.friendtoid FROM Users a, Friends b WHERE request=1 AND friendfromid = a.userid
Vice versa for friendfromid.
Use a UNION query, joining to friends as below
SELECT username FROM user INNER JOIN friends ON (userid = friendfrom) WHERE friendto = {myid) AND request = 1
UNION
SELECT username FROM user INNER JOIN friends ON (userid = friendto) WHERE friendfrom = {myid} AND request = 1
Remember you need indexes on the friendto and friendfrom columns for performance