Getting two different results by one SQL query - php

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)

Related

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

Ordering By Another Table Column If Exists

I'm putting together a simple forum script with topics/posts. I'm trying to order topics by latest post and I've had some luck with the answer in the following question: MYSQL Order from another Table, but the problem I have is that some topics don't have any posts yet, so the query doesn't select those topics at all.
Current coding is:
SELECT DISTINCT forum.*
FROM forum
INNER JOIN forum_posts
ON forum.id = forum_posts.parent_id
GROUP BY forum.id
ORDER BY forum_posts.parent_id DESC,
forum_posts.time_created DESC
LIMIT ?,?
I want to tell the ORDER BY to order by forum.time_created if there are no matches in forum_posts.parent_id for a topic.
On a side note, I would also like to know how to put a WHERE clause into this query as well. I want to only get rows from forum table "WHERE access <= ?", but can't work out where to put that snippet in.
Any help much appreciated!
EDIT:
Target is to return topics (from forum table)
According to following details:
WHERE forum.access <= ?
LIMIT ?,?
ORDER BY
Latest Post From forum_posts table with forum_posts.parent_id matching on forum.id,
or forum.time_created
EDIT 2:
An example SQLfiddle with relevant data. This doesn't work as the order should really come out as 11,10,9,1,2
http://sqlfiddle.com/#!2/83535/2
Have a look at this: http://sqlfiddle.com/#!2/be909/1
Here's my final query:
SELECT forum.*, recent_forum_posts.time_created as latest_post
FROM forum
LEFT JOIN (SELECT MAX(forum_posts.time_created) as time_created, forum_posts.parent_id
FROM forum_posts
GROUP BY forum_posts.parent_id) AS recent_forum_posts
ON forum.id = recent_forum_posts.parent_id
WHERE forum.access > 2
ORDER BY recent_forum_posts.time_created IS NULL DESC,
recent_forum_posts.time_created DESC,
forum.time_created DESC
LIMIT 5
Essentially, the subquery (the bit in brackets after LEFT JOIN) selects the most recent post for each parent_id from the forum_posts table.
That is then LEFT JOINed (as we want to list all forum's even if there are no posts yet).

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.

Ordering a mysql query?

I have 2 tables, Pages and LinkPages.
Within Pages i have the fields:
pageID (the identifier of the page),
startmemberID (the id of the member that created the page),
startDate (date the page was created).
In LinkPages I have:
pageID (to link with the page),
linkmemberID (member linking with the page),
joinDate (date member linked with the page).
What sql query would i use to get all information on the pages with a particular id and then order it by the date the page was started.
I got this far:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID' LIMIT 5
but obviously i haven't ordered them here, would i need to use a join?
Many Thanks,
Jai
Try this:
SELECT * FROM LinkPages
INNER JOIN Pages ON Pages.pageID = LinkPages.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY startDate DESC
LIMIT 5
SELECT lp.pageID, lp.linkmemberID, lp.joinDate
FROM LinkPages lp, Pages p
WHERE lp.linkmemberID='MEMBERID' AND lp.pageID = p.pageID
ORDER BY p.startDate DESC
LIMIT 5
You have two options, either you can JOIN or you can use a subquery:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID'
ORDER BY
(SELECT startDate FROM Pages WHERE Pages.pageID = LinkPages.PageID) DESC
LIMIT 5
For good measure, here's the join:
-- be sure to use L.* here, otherwise you get all of the columns from
-- pages as well
SELECT L.* FROM LinkPages L
INNER JOIN Pages P ON P.pageID = L.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY P.startDate DESC LIMIT 5
SELECT lp.*
FROM LinkPages lp, Pages p
WHERE lp.pageId = p.pageId
AND lp.linkmemberID='MEMBERID'
ORDER BY p.startDate
LIMIT 5
sorry - forgot the ORDER BY ...

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