Show my posts as well as followers posts in PHP and MySQL - php

I'm trying to make a homepage somewhat like Facebook, I made it so it could show the posts from the people I follow, but I couldn't see my own posts as I can't follow myself. Here is the line of SQL code I've written (it contains PHP variables):
SELECT *
FROM user_posts
INNER JOIN user_following ON user_posts.username = user_following.username
WHERE user_following.follower = '$me->username'
ORDER BY id DESC
LIMIT 0, 15
The user_posts table contains all the posts.
The user_following table contains all follow data, where username is the user being followed, and the follower is the user following the username
$me->username is the username of the user logged in.
user_posts table structure:
user_following table structure:
Thanks, in advance!

There's a couple of different ways to skin this query:
Sub-query
SELECT *
FROM user_posts
WHERE user_posts.username = 'bob'
OR user_posts.username IN(
SELECT username
FROM user_following
WHERE user_posts.username = user_following.username
)
LIMIT 0, 15
http://sqlfiddle.com/#!9/6bf2c6/9
Use the Users Table
Requires GROUP BY or DISTINCT user_posts.id, which are non-optimal.
SELECT
user_posts.*
FROM users
LEFT JOIN user_following ON users.username = user_following.username
INNER JOIN user_posts ON (
users.username = user_posts.username
OR user_following.follower = user_posts.username
)
WHERE users.username = 'bob'
GROUP BY user_posts.id
LIMIT 0, 15
http://sqlfiddle.com/#!9/d91be/1
IMPORTANT! Make sure and index those columns in your table. Otherwise, performance will suffer as the tables get bigger (especially user_following).

Try this code:
select *
from user_posts up
join user_following uf on up.username = uf.username
where uf.follower = '$me->username'
or up.username = '$me->username'

Related

SQL get all users activity summed in a query

I want get all users from users table and have a column summed up on all articles length written by all users from article table
Basically
Here is a code if I choose to run an SQL query for each loop (not good)
SELECT * FROM users
--loop
SELECT SUM(wordcount) AS totalwordcount FROM articles WHERE writer_id = user_id
I have used LEFT JOIN but I can only get a list of users with their user_id present on article table
SELECT users.name, SUM(article.wordcount) AS xox FROM users LEFT JOIN article ON users.id_usera = article.writer_id WHERE article.editor_status = 'finished' ORDER BY users.id_usera
With this query, I only get users with their user_id present in the article table but I want all users listed and NULL/empty returned if the user has no article with his/her id present
You need GROUP BY:
SELECT u.name, SUM(a.wordcount) AS xox
FROM users u LEFT JOIN
article a
ON u.id_usera = a.writer_id AND
a.editor_status = 'finished'
GROUP BY u.name;
SELECT users.name, SUM(article.wordcount) AS TotalWordCount
FROM users
LEFT JOIN article ON users.id_usera = article.writer_id
WHERE article.editor_status = 'finished'
GROUP BY article.writer_id
ORDER BY users.id_usera

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)

top first friends then the other users from like list

I have tables:
likes - id, user_id, like_user_id<br>
users - id, name, email ...,<br>
friends - id, user_id, friend_id, status<br>
Is it possible to sort it with one SQL query first to show the friends then the other users.
Any help would be appreciated.
Thank you.
I tried this and it works, but the problem is it give me double results of users:
select *
from `likes`
left join `users` on `users.id` = `likes.user_id`
left join `friends` on `friends.user_id` = `likes.user_id`
or `friends.friend_id` = `likes.user_id`
where `likes.id` = 1
order by `friends.user_id` = 5
or `friends.friend_id` = 5
You need to work with a UNION here to merge the liked users with the befriended users. Upon doing this, you can create an artificial column friend, that you fill with 1 in the friend query and 0 in the like query. Later on you can order by that column.
SELECT
friends.user_id,
1 as friend,
users.*
FROM
friends
JOIN users ON users.id = friends.friend_user_id
UNION SELECT
likes.user_id,
0 as friend,
users_liked.*
FROM
likes
JOIN users as users_liked ON likes.like_user_id = users_liked.id
WHERE
user_id = '$userId'
ORDER BY friend DESC, id ASC
This will return a list of all friends, followed by a list of all liked users.

How to get posts from people I follow and my posts in php and mysql?

I have table
answers(aid,user_id,...) - posts table
users (userid,name,...)
followers(user_one,user_two) - where user_one is the follower and user_two is the followed by person.
I need to show posts from people I follow and my posts using php and mysql in my home feed.But currently it is not working as expected.Each posts is showing 7 times.
curent select query
<?php
$my_id = $_SESSION['user_id'];
$sql_query = "SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC";
?>
Didn't see you also want to return your posts. Try below sql:
select * from answers, users, (select user_one user_id from followers where user_two='$my_id' union select '$my_id' user_id) a where answers.user_id=users.user_id and answers.user_id=a.user_id;
There is error in your sql: SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC".
the second left join should be followers.user_two=ansers.user_id, and the where statement should be followers.user_one='$my_id';
One option here is to phrase your query as a union of two queries. The first half of the union below finds all posts given by people you follow. The second half of the union finds all of your posts.
SELECT a.*
FROM answers a
INNER JOIN followers f
ON (a.user_id = f.user_two AND f.user_one = $my_id)
UNION ALL
SELECT a.* FROM answers WHERE user_id = $my_id;
I'm not a PHP person, but hopefully you can easily adapt the above query into your code.

SQL: Check if value is available in tbl1 or tbl2

I want to create a sub-account system (PHP & MYSQL).
I have a user table (users) and a sub users table (sub_users).
How can check if the user is available in the user table, or in the sub users table?
My code:
SELECT DISTINCT *
FROM users
WHERE userid = "steven"
OR WHERE EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = "steven");
ERROR: Check your syntax near "steven"
Also tried:
SELECT *
FROM users
LEFT JOIN sub_users
ON sub_users.user_userid = users.userid
WHERE users.userid = 'steven'
OR sub_users.userid = 'steven'
Same error.
In the first place, you only want one where clause. I would also use single quote instead of double quotes:
SELECT DISTINCT *
FROM users
WHERE userid = 'steven' or
EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = 'steven'
);
I doubt you need the distinct keyword, if you are fetching all the columns from users.
Your second query looks ok. Are you sure you are not running the first query twice?
EDIT:
I'm trying to figure out what you want to return. The following returns 1 if 'steven' appears in either table and 0 otherwise:
select (case when exists (select 1 from users where users.userid = 'steven') and
exists (select 1 from sub_users where sub_users.userid = 'steven')
then 1
else 0
end);
This method saves on the overhead of a join and will readily take advantage of indexes on users(userid) and sub_users(userid).
You could also use a union:
(SELECT userid FROM users WHERE userid = 'steven')
UNION
(SELECT userid FROM sub_users WHERE userid = 'steven');
select * from
(select * from users) x,
(select * from sub_users) y
where x.user_id = 'steven' or y.user_id = 'steven'
Good luck !!!

Categories