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:
Related
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
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
I am attempting to get the latest threads that have been posted on in a Forum with vars from Users, Threads and the last Post. Problem is that the current method i am attempting brings back duplicate threads because the newer posts have been posted in those threads, whereas i just want one post to return per thread, not all the latest posts.
SELECT t.thread_id, u.user_name, p.post_entry
FROM forum_thread as t
LEFT JOIN forum_post AS p ON p.post_thread = t.thread_id
LEFT JOIN user AS u ON u.user_id = p.post_user
ORDER BY t.thread_lastpost DESC LIMIT 0,8
Currently that is returning:
/-----------------------------------------/
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7650 | USERNAME | Post Entry |
|----------------------------------------|
| 7068 | USERNAME | Post Entry |
|----------------------------------------|
| 7056 | USERNAME | Post Entry |
|----------------------------------------|
| 7136 | USERNAME | Post Entry |
I want to remove those first duplicate IDs and only leave one with the latest post entry from that thread.
I hope i have explained it well enough for people to understand.
Thanks.
----------------- EDIT --------------------
Got it to work with GROUP BY:
SELECT t.thread_id, u.user_id, p.post_entry
FROM forum_post AS p
LEFT JOIN forum_thread AS t ON t.thread_id = p.post_thread
LEFT JOIN user AS u ON u.user_id = p.post_user
GROUP BY t.thread_id
ORDER BY t.thread_lastpost DESC LIMIT 0,8
this query may not be best performing, but I can't do much more without knowing structure of your database. You'll need some post_id or post_timestamp for including the second left join as well. Neither DISTINCT nor GROUP BY would solve your issue, as the username and post_entry will usually be different in all cases, i.e. the lines won't actually be distinct.
SELECT t.thread_id, u.user_name, p.post_entry
FROM forum_thread as t
LEFT JOIN forum_post AS p ON p.post_thread = t.thread_id
LEFT JOIN forum_post AS p2 ON p.post_id > p2.post_id
LEFT JOIN user AS u ON u.user_id = p.post_user
where p2.post_id is null
ORDER BY t.thread_lastpost DESC LIMIT 0,8
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
I'm trying to make an application that pairs two available users by selecting them randomly from a database and then making them unavailable (so they can't be selected again).
I was wondering how I could select them and then pair them?
I've got the following database structure.
USERS
| id | autoincrement, primary key
| user_id | user's ID
| connected | available, connecting, chatting
ROOMS
| id |
| room_name |
| room_id |
| user_id | first user connected
| user_id2 | second user connected
I'm not making rooms, I'm simply trying to pair users and send them to a room.
Do you mean join the records?
You could do something like:
select * from rooms as r
left outer join users as u on r.user_id=u.id
left outer join users as u2 on r.user_id2=u2.id
where r.id=123
123 = a room id.
EDIT:
Here is a way to update a Rooms record with two random Users
UPDATE rooms
SET
user_id=(SELECT id FROM users ORDER BY RAND() LIMIT 0,1),
user_id2=(SELECT id FROM users ORDER BY RAND() LIMIT 0,1),
where id=123
I have tested this and it seems to work. This will keep you from having duplicate users:
UPDATE rooms
SET
user_id=(SELECT u1.id FROM users as u1 ORDER BY RAND() LIMIT 0,1),
user_id2=(SELECT u2.id FROM users as u2 where user_id <> u2.id ORDER BY RAND() LIMIT 0,1)
where id=123
The SELECT could be something like this:
SELECT user_id
FROM `USERS`
WHERE connected = 'available'
ORDER BY RAND()
LIMIT 2;
You would need to parse the resultset and assign the to user_ids to variables such as
$user_id1
$user_id2
The first UPDATE would be something like this:
UPDATE `USERS`
SET connected = 'connecting'
WHERE user_id IN ($user_id1,$user_id2);
The INSERT would look something like this:
INSERT INTO `ROOMS` (room_name,room_id,user_id,user_id2)
VALUES ($room_name,$room_id,user_id,user_id2);
Finally the last UPDATE
UPDATE `USERS`
SET connected = 'connected'
WHERE user_id IN ($user_id1,$user_id2);
Although you are probably going to want to lock rows I have not grasped that part of database management yet. For you purposes you might want to look into it.
According to me you'll need three tables instead of two to implement your logic.
Consider my db schema
USERS
| id | autoincrement, primary key
| user_id | user's ID
| connected | available, connecting, chatting
ROOMS
| id |
| room_name |
USERCHAT
| id |
| user_id | user's ID
| room_id | room id
To get pair you can execute following query.
SELECT user_id
FROM USERS
WHERE id NOT IN (SELECT user_id FROM USERCHAT)
ORDER BY RAND()
LIMIT 2;
After selecting users you can simply insert them in ROOMS and USERCHAT tables. Using this schema you can also allow multiple users instead of two for chat.