Getting Value From A Third Table - php

SELECT POSTS.*
FROM POSTS
LEFT JOIN SUBSCRIBERS
ON POSTS.AUTHORID = SUBSCRIBERS.PROFILEID
WHERE POSTS.AUTHORID = ?
OR SUBSCRIBERS.SUBSCRIBERID = ?
ORDER BY POSTID DESC
LIMIT ?
The above is the query I'm using and this will select posts from the profiles a user is subscribed to. Now this works fine, but now I want to grab another column from another table.
In my third table USERS, I have an "AVATARID" which I want to access.
In my fetch loop for this query, it echos out the username of the post author, and the post body. What I want it to do is also echo out the avatarID of the user who wrote the post. I tried adding in another query inside my while loop, but I find that sloppy, and it also doesn't work :S
So, simple question: How do I access the AvatarID from the table USERS with the USERID of the AUTHORID from the post?

Something like...
SELECT POSTS.*, USERS.AVATARID
FROM POSTS
LEFT JOIN SUBSCRIBERS ON POSTS.AUTHORID = SUBSCRIBERS.PROFILEID
JOIN USERS ON POSTS.AUTHORID = USERS.USERID
WHERE POSTS.AUTHORID = ?
OR SUBSCRIBERS.SUBSCRIBERID = ?
ORDER BY POSTID DESC
LIMIT ?

Related

Get data from users(table) Where id of users(table) equals to id of friends(table)

I've tried to get data from from one table if id of 2 tables equals to each other. Here is the code which I used:
SELECT id_to
, email_to
, name_to
, status_to
FROM users
LEFT
JOIN friends
ON users.id = friends.id_from
WHERE id_from = ?
I used LEFT JOIN to join two tables but it gets the values from the friends(table) instead of users(table).
I think I've explained my problem clearly.
I guess you must specify it on your query, like this:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE id_from = ?
You can do the same if you need to retrieve values from 'friends' table.
If both tables have the same column, then you can specify the table name while selecting columns. so your code will look like:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE friends.id_from = ?

What Sql Join can I use to combine my three tables

I made a query that user can see all the post of active users but not of those inactive, but also they should not see the post of users they blocked or blocked them how can I do that?, the block_users table has block_by_userid and blocked_userid column.
I tried INNER JOIN the post table with members table which every inactive users post cannot be seen anymore., How will I combine the block_users table?
$GetPost = "SELECT Post.* FROM Post
INNER JOIN Members ON Post.Poster_user_id = Members.User_id
WHERE Status='active'";
you might try FULL OUTER JOIN
the idea it to get all items in both tables member and block_users and then filter the data using where clause, i am sorry if i miss using the fields name , so please check the tables names, i assume block_users.block_by_userid contained the blocked user id if not use the correct field
$GetPost = "SELECT Post.* FROM Post
INNER JOIN Members ON Post.Poster_user_id = Members.User_id
FULL OUTER JOIN block_users ON Members.User_id = block_users.user_id
WHERE Status='active'
AND
WHERE block_users.block_by_userid != Members.User_id";
Suppose I am doing this for a user xyz whose user_id is 123 then below is a query which will return expected output for user 123.
Query:
SELECT Post.* FROM Post
INNER JOIN Members ON Post.Poster_user_id = Members.User_id
WHERE Status='active' and post.poster_user_id NOT IN(select block_by_userid from block_users where blocked_user_id = 123 UNION select blocked_user_id from block_users where block_by_userid = 123)

Select from multi tables and return all

I have 4 tables:
posts [post_id post_title post_body post_date post_by post_accepted]
users [user_id user_name user_pw]
comments [comment_id comment_user comment_co comment_post comment_date]
categories [categorie_id categorie_name]
Each table contain a data all data are belong to table posts:
categories<-posts
posts<-users
posts<-comments<-users
I want to get posts with id post_id and get with it a comments and who post it and get the category name and id.
I tried but I get the post but not all comments or if the post does not have comment it will no appear.
This my SQL query:
SELECT COUNT(comment_id),comments.*,categories.*,users.*,posts.*
FROM posts
JOIN categories on (posts.post_id = categories.categorie_id)
JOIN users on (posts.post_by = users.user_id)
LEFT JOIN comments on (posts.post_id = comments.comment_post)
WHERE posts.post_id='34'
AND posts.post_accepted = '1' ;
Try add GROUP BY users.id to your query

SQL with multiple joins with same tables

I'm building a forum, and I have a problem with a SQL select with many joins. I want to show two images of different users in the same row.
The first image of the user is who wrote the topic, and the second image is of the user who last replied.
The query I build:
SELECT
posts.*, users.photo, users.displayname FROM posts
JOIN users ON(posts.useraid = users.id)
JOIN users ON(posts.lastreply = user.id)
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC
posts.lastreply = the ID of the last reply user.
You have to specify an alias for each table using the AS keyword:
SELECT posts.*,
u1.photo AS creatorPhoto, u1.displayname AS creatorName,
u2.photo AS replierPhoto, u2.displayname AS replierName
FROM posts
JOIN users AS u1 ON(posts.useraid = u1.id)
JOIN users AS u2 ON(posts.lastreply = u2.id)
WHERE forumid= #forumid and type='post'
ORDER BY `timee` DESC
Notice how I call each instance of the users table by a different name - u1 and u2. Also notice how I have specified a column alias to distinguish between the two columns of the same name (e.g. creatorPhoto and replierPhoto). This way you can use the name as an index into a PHP associative array a la $post['creatorPhoto'].
Yes, I've silently changed your inline variable to a parameter. Take it as a hint. :-D
In addition to the lack of aliases in the from clause you may also have a problem with the where and order by clause. You need to use aliases for the columns there.
I don't know where they come from, but something like:
WHERE posts.forumid='$forumid' and posts.type='post'
ORDER BY posts.`timee` DESC
Assuming all come from posts.
you need an alias for this to work
SELECT
posts.*, u1.photo, u1.displayname, u2.photo, u2.displayname FROM posts
JOIN users u1 ON posts.useraid = u1.id
JOIN users u2 ON posts.lastreply = u2.id
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC
SELECT posts.*, author.photo as author_photo, author.displayname as author+name,
replier.photo as replier_photo, replier.displayname as replier_name
FROM posts
JOIN users author ON(posts.useraid = users.id)
JOIN users replier ON(posts.lastreply = user.id)
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC

Issue in mysql Join query

I am developing a social website.I have an option called alerts which shows pending friend requests and unread messages.My query is following:
SELECT
sk_friends.frndship_from_user_id,
sk_messages.msg_from_user_id,
sk_messages.msg_text,
sk_messages.msg_date
FROM
sk_friends INNER JOIN sk_messages
WHERE
sk_messages.msg_to_user_id = '$user_id'
AND sk_friends.frndship_to_user_id ='$user_id'
AND sk_friends.frndship_status = 'pending'
AND sk_messages.msg_status='unread'
ORDER BY
sk_friends.fndship_date ASC,
sk_messages.msg_date ASC;
sk_friends and ak_messages are tables.
msg_from_user_id is the id of sender
frndship_from_user_id is the id of the user who sends the request
$user_id is the id of the login user
Each row data is appearing twice. I dont know why does it happen.
Your inner join does not have an ON clause. Try adding one, that should remove double results.
Try this :
SELECT
sf.frndship_from_user_id,
sm.msg_from_user_id,
sm.msg_text,sm.msg_date
FROM
sk_friends sf ,
sk_messages sm
WHERE
sm.msg_to_user_id = sf.frndship_to_user_id AND
sm.msg_to_user_id = '$user_id' AND
sf.frndship_status = 'pending' AND
sm.msg_status='unread'
ORDER BY
sf.fndship_date ASC,sm.msg_date ASC;

Categories