Mysqli - How to build the Query? - php

I try to request my Database so i will get like in Facebook a News Feed (newest Posts) on the Home Site.
I have a Table with Friends- userid1 - userid2- accepted
So if accepted == 1 they are Friends ...
I already tried to Request first the Friends and then get the Posts... Wrong way cause at least they shown it in the Right way with ordered by timestamp...
But in blocks for each friend...
not ordered only by Timestamp ...
SELECT * FROM friends
WHERE user1='$ownid' or user2='$ownid'
AND accepted='1'
My Post table:
Userid- from_user_id- timestamp- text
SELECT * FROM posts
WHERE from_user='$friendsuserid'
ORDER BY RAND() , timestamp desc
So my Question,
What is the exact query to get fast all post from my Friends and order them by timestamp ?
Means-> Check whos friedn-> getPosts -> order by Timestamp
and all this in 1 Query.... ?
Thanks for every Answer.

Try this:
SELECT * FROM posts p INNER JOIN friends f ON
((p.from_user=f.user1 OR p.from_user=f.user2) AND (f.user1='$ownid' OR f.user2='$ownid') AND f.accepted)
WHERE p.from_user!='$ownid' ORDER BY p.posted_time DESC

Related

Getting two different results by one SQL query

First I am new to SQL and PHP.
I have created a simple social networking web app so users can post and follow others to see new posts from them.
At home page a user can first see posts from all users he is followong.
but what i want is to make the user see some other random popular posts that will be ordered by Likes.
here what i have done to get posts from users i follow:
SELECT * FROM posts WHERE author_id in
(SELECT followedID FROM follows WHERE
followerID=:myID)
ORDER BY id DESC LIMIT 10
Now let's say you are following only 1 person. and that person has only one post. here you will see no more than a post!
That's why i want to show more posts when a user has already seen all posts.
i want some easy way to get other posts when the above query has done getting some specific posts.
This is the next query i'd like to execute.
SELECT * FROM posts ORDER BY post_likes DESC LIMIT 10
I wouldn't recommend union, because it incurs overhead for removing duplicates.
Instead, you can use a LEFT JOIN:
SELECT p.*
FROM posts p LEFT JOIN
follows f
ON p.author_id = f.follows_id AND
f.followerID = :myID
ORDER BY (f.follows_id IS NOT NULL) DESC,
(CASE WHEN f.follows_id IS NOT NULL THEN p.id END),
p.post_likes DESC
LIMIT 10;
The ORDER BY puts the followed posts first. The other two clauses order each of the groups by the criteria you want.
You may use UNION to do what you want
(SELECT * FROM posts WHERE author_id in
(SELECT followedID FROM follows WHERE
followerID=:myID)
ORDER BY id DESC limit 0,10)
union
(SELECT * FROM posts ORDER BY post_likes DESC limit 0,10)
LIMIT 0, 10
UNION will automatically append the 2nd query result to the 1st query result, and then show only the number of records specified by the LIMIT clause
Please note that union works only if the queries are of the same structure (which in this case is positive)
Please note that the use of parenthesis is mandatory if you use order by or limit or both
I have used 3 limit clauses (one for each query , and one for the final result of union) AND Both queries have ORDER BY clause. This is to make sure that the records extracted are what you want. (to show the followed posts first, and both are ordered properly)

Select all from table where value=multiple values from another table

I'm trying to select posts that are posted by somebody the user is following.
My posts table looks like this:
My followers table looks like this:
The "creator" column in the posts table is the ID of the user who posted it. The "follower" colum in the followers table is the user who is following the "following" column.
I'm using this query (to no avail) to select all from posts where creator=all users the user is following:
SELECT * FROM posts WHERE creator=(SELECT following FROM followers
WHERE follower=the user's id) ORDER BY datetime DESC;
In theory:
SELECT * FROM posts WHERE creator=1 or 2 or 3 or 4...or 998 or 999
etc, etc.
What would be the best way to do this?
Thanks!
Try changing the query to:
SELECT * FROM posts WHERE creator in (SELECT following FROM followers WHERE follower=the user's id) ORDER BY datetime DESC;
Using joins in this case can boost up the performance of your query. I recommend the following:
SELECT * FROM posts p
INNER JOIN followers f
ON p.creator = f.following
WHERE f.follower = <user_id>
ORDER BY datetime DESC;
Try it
SELECT *
FROM posts p, followers f
WHERE
p.creator=f.following
and f.follower ='user_id'
ORDER BY p.datetime DESC;

Get the list of ten top publishers in database

I've got following tables in my MySQL database:
USERS
iduser nick password
ARTICLES
idarticles iduser text
How can I get by one SQL query the list of e.g. 10 top publishers of articles ordering by the count of added articles? Is there any way to do that? I'm using PHP to ask database.
Yes, this should be quite easy via JOIN and COUNT(). Something like the following
SELECT `users`.`iduser`, COUNT(`articles`.`idarticles`) AS `total_articles`
FROM `users`
INNER JOIN `articles` ON `users`.`iduser` = `articles`.`iduser`
GROUP BY `users`.`iduser`
ORDER BY `total_articles` DESC
LIMIT 10
A little explaining:
COUNT() will get you what it says - a count of all relevant entries in articles
INNER JOIN will pair all entries that belong together (defined via ON)
GROUP BY tells SQL that you are interested in various results, each differing by iduser (without this, you'd get all articles counted in the first returned row)
ORDER BY .. DESC is important to get the result in a descending order (so most published first)
LIMIT 10 does exactly that

PHP MYSQL search based on rating and timestamp

My search query runs like:
select * from posts p where p.post like '%test%' ORDER BY p.upvotes DESC,
p.unix_timestamp DESC LIMIT 20
If there are more than 20 results for the searched keyword, i find out the minimum timestamp value, store it in a hidden element and run another query to Load More results like:
select * from posts p where p.post like '%test%' and p.unix_timestamp < 1360662045
ORDER BY p.upvotes DESC, p.unix_timestamp DESC LIMIT 20
Whats really happening is that my first query is ignoring (Obviously, my mistake) posts which haven't had any votes(meaning 0 votes) because of my ORDER BY p.upvotes DESC and as a result of this, i noticed that it fetched the first post in the table in the first 20 results, so the minimum timestamp becomes first post's timestamp. Now after this, if i try to fetch the next 20 results which is less than the minimum timestamp, it doesn't give anything.
Right now, i am simply using the upvotes ordering to fetch top records. Should i be using some algorithm like Bayesian Average or some other algorithm?
Please advise how i can improve the queries if i had to stay with current system of ordering or is there any viable and more efficient method i should be using?
P.S. If possible, please refer some resources about the Bayesian Average(it seems to be most used) or some other alternative?
Storing the timestamp when you first do a search and then using that for the next query you could use something like this:-
$sql = "SELECT *
FROM posts p
LEFT OUTER JOIN (SELECT post_id, COUNT(*) FROM post_ratings WHERE timestamp_rated <= $SomeTimeStoredBetweenPages GROUP BY post_id) pr ON p.id = pr.post_id
WHERE p.post like '%test%'
ORDER BY pr.post_ratings DESC, p.unix_timestamp
DESC LIMIT ".(($PageNo - 1) * 20)." 20";
This is very much an example as I have no real idea of you table structures. Also not sure if you just have a row for each up vote, or whether there are down votes to take account of as well.

help me in this query

i am trying to develop a site where users can become friends of each other and can see their posts after the date the became friends........i have already posted this question about 5-7 days but could not find any solution........!!
so...
i have two tables..........
posts and friends
and my query is
$sql = mysql_query("
SELECT *
FROM posts p
JOIN friends f
ON p.currentuserid = f.friendid
AND p.time >= f.friend_since
OR s.currentuserid=$myid
WHERE f.myid=$thisid
ORDER BY p.postid DESC LIMIT 20");
where $myid is currentuserid and p.currentuserid is the name of cell in poss table and friendid is in friends table.
this query is working all the way right but problem is that in this query if current user post any thing it displays two times i.e
my new post
mynew post
but in database it is single entry.....!! but current user can see their friends posts for single time
how can i solve this problem
this query is working all the way right but problem is that in this query if current user post any thing it displays two times
Use:
SELECT DISTINCT *
FROM posts p
JOIN friends f ON p.currentuserid = f.friendid
AND p.time >= f.friend_since
OR s.currentuserid=$myid
WHERE f.myid=$thisid
ORDER BY p.postid DESC
LIMIT 20
I added the DISTINCT keyword in order to remove duplicates. Usually I'd use a GROUP BY instead, but you didn't supply the columns.

Categories