Retrieve latest post in each category - Forum - php

I'm building a forum, and I have ran into a few problems.
The basic database structure looks like this:
users
| user_id | username
categories
| category_id | category_name |
forum_posts
| post_id | ref_post_id (FK) | ref_category_id (FK) | ref_user_id (FK) | post_date |
If ref_post_id is 0 that means it's the main post of the thread that have a title. For answers to a thread ref_post_id equals the main post's post_id. I hope you understand.
How would I get the latest post in each category? Including the posts thread title, and the username from user table. Should I change my table structure and add a "latest_post_id" field to categories table or something?
Very greatful for your help. I know there are similar questions, but I'm also wondering about whether I should store latest_post_id and all that in categories table or have a huge query for retrieving everything on each page load.
EDIT 2: HERE IS MY CURRENT QUERY:
SELECT category_id, name,
(SELECT COUNT(*) FROM forum_posts WHERE ref_category_id = category_id AND ref_post_id = 0) count_threads
(
SELECT title, ref_user_id, username FROM forum_posts
LEFT JOIN users ON user_id = ref_user_id
WHERE latest_post_id = (SELECT MAX(latest_post_id) FROM forum_posts WHERE ref_category_id = category_id LIMIT 1)
)
FROM forum_categories

if you have a creation date you need to get the one with the MAX date, if you don't you can use the MAX(post_id), but if you let user EDIT their post and you want to get the latest one created OR edited you should add a modification date to the database.
to get the latest post:
SELECT * FROM forum_posts p
INNER JOIN users u ON p.ref_user_id=u.user_id
WHERE `post_id`=(SELECT max(`post_id`) FROM forum_posts WHERE ref_category_id=$value);
If you are using a date, just use that field instead of post_id

Related

Show publications according to the interests of the users

I want to show publications according to the interests (in keywords) of the users.
USERS TABLE
id | username
5 | joelsilva
PUBLICATIONS TABLE
id | publication title
8 | The best car of the year!
TABLE OF INTERESTS
id | username | interest
8 | joelsilva | car
9 | joelsilva | year
It shows all the publications with the car title and year on the home page.
I've tried this:
$pubs = mysql_query(
"SELECT *
FROM PUBLICATIONS
WHERE (interest LIKE '%".$interests."%')
ORDER BY id desc"
) or die(mysql_error());
It works, but only with one type of interest in the table. If adding more than one interest shows nothing.
Join the publications and the interests on the title containing the interest and get the distinct rows of publication. Filter for the user.
SELECT DISTINCT
p.*
FROM publications p
INNER JOIN interests i
ON p.title LIKE concat('%', i.interest, '%')
WHERE username = ?
ORDER BY p.id DESC;
(BTW: Instead of the user's name, their ID should be in the interests table. And you should probably have a look here to learn why you shouldn't use string concatenation when building queries but parameterized queries.)

Like structure table with Laravel and showing the most popular content at top

I am using Laravel to creating a website, my users can post questions and other users can write their comments under the post, each comment have Up vote and Down vote, and users can voting for comments.
I need most liked (Up vote) shows topper than others..
This is my database structure and I join them together:
comment table:
comment_id | question_id | user_id | timestamp
up and down votes table (like):
like_id | comment_id | like_type | user_id | timestamp
note:like_type is an enum on mysql and its values are upvote and downvote.
1-What is the mysql query and Laravel codes for that?
2-Is my database Structure right?
1.Calculate SUM belongs to Each Comment
2.Make it order by Desc
select * from (
select ct.comment_id,ct.question_id,
ct.user_id,SUM(case when vt.like_type='upvote' then 1 else -1 end )
cnt from commenttable ct
from votestable
vt left join
on
vt.comment_id=ct.comment_id
)D order by D.cnt desc

How to get one status update from users

I have a question about MySQL Query. I have a slider on the homepage to show the newest user status updates. I am using JOIN table to do this. Everything work well expect the status update is not recognize with user own status.
There is my table row:
users
id | username | name | last_login | status | date_registered
posts
userid | post_details | date_posted | status | facebook | twitter
There is my query:
"SELECT U.id, U.username, U.name, U.last_login, U.status, P.userid, P.post_details, P.date_posted
FROM users U
LEFT JOIN posts P ON U.id = P.userid
WHERE U.status='active' AND P.status='1'
ORDER BY U.last_login DESC,
LIMIT 12"
Problem when I am using above query's user status update not recognize with ordered user slider. When I add AND P.userid=U.id in WHERE line, only 1 user is displayed.
Please tell me a better way to make status updates is recognized with user.
Preview:

mysql sub query to get latest comment from two tables

I have been working on a query to get summarised list of communications like an inbox that shows conversations
below are my tables
users
company | contact_person | pic_small
alerts
comment_id | user_id | poster_id | timestamp
activity
comment_id | user_id | comment | timestamp
comments
comment_id | user_id | comment | timestamp
This is what I have so far which works ok although I need help with one aspect.
SELECT alerts.comment_id,
alerts.user_id,
alerts.poster_id,
alerts.active,
MAX(alerts.timestamp) AS maxTime,
users.contact_person,
users.company,
users.pic_small
FROM alerts
LEFT JOIN users ON users.user_id = alerts.poster_id
WHERE alerts.user_id = %s
GROUP BY alerts.comment_id
ORDER BY maxTime DESC
Comments are held in the activity and comments tables and I need to somehow join the last (newest) comment from either the activity or comments table (depending which is newer)
How do I add this to my above query, below is what I am trying to achieve

Mysql: Many to Many query based on Array

I have a database with 4 tables:
users following
------------- --------------------------------
| id | etc.. | | user_id (FK) | follow_id (FK) |
articles article_relations
------------- --------------------------------
| id | etc.. | | user_id(FK) | article_id (FK) |
Users can follow other users so they can see what articles they saved.
So based on the current user id it needs to check which users he/she follows in the table "following". The users that are followed will be known now.
Based on those follower ids it needs to check in article_relations which article id's are linked in article_relations to the followers. Now it should now which articles are saved by which users.
So now in the table articles the article_ids needs to be queried keeping the information which user the article saved.
Is the database design logical?
It seems to me that querying the "stream" to see which articles the users you follow saved is very redundant.
Can someone tell me if the DB Design is OK and help me with the query to get the articles from
the users you follow including the user info?
I tried till my pants fell off and searched whole stackoverflow!
try this query, it'll get you user's information, and the people the current user is following's information and the articles that are saved by the people the current user is following.
SELECT u.*,
fu.*,
a.*
FROM following f
INNER JOIN users u ON u.id = f.user_id
INNER JOIN users fu ON fu.id = f.follow_id
INNER JOIN article_relations ar ON ar.user_id = f.follow_id
INNER JOIN articles a ON a.id = ar.article_id
WHERE f.user_id = <currentUserId>
just change the .* to fields you need from each table.
Structure looks fine to me. As for the query, try this.
Join the following table (a) to the article_relations table (b)
by way of the follow_id then joining that to the article table (c) by article_id all based on the current user id.
I'm including the fictional fields article_name and article_field just as an example of what you might be pulling from the article table.
select c.id,
c.article_name,
c.article_field
from (following a
join article_relations b on a.follow_id = b.user_id)
join articles c on b.article_id = c.id
where a.user_id = current_user_id;
Just need to add the users table (d) into the mix for that.
select c.id,
c.article_name,
c.article_field,
d.id
from ((following a
join article_relations b on a.follow_id = b.user_id)
join articles c on b.article_id = c.id)
join users d on a.user_id = d.id
where a.user_id = current_user_id;

Categories