MYSQL select newest posts from tables - php

I want to select the newest posts from users, I am looking for the most efficient way to do this.
Currently this selects the first post, not the last:
$query = mysql_query("
SELECT *
FROM posts
WHERE toID=fromID
GROUP BY fromID
ORDER BY date DESC LIMIT 3");
Table Structure:
Table: posts
id ToID FromID Post State Date
1 1 1 Hey 0 1325993600
2 1 6 okay yeah 0 1325993615
3 1 2 again 0 1325994600
4 6 6 yeah2 0 1325995615
so from this above example it would return id: 1 and 4.
toID=fromID is just to get the post that is a status message, meaning the user posted something on their own page, not someone elses.
I want to get the most recent status from the last 3 users that have updated their status.

The ID thing would still work theoretically, provided that the ID's never change...
I would recommend using a timestamp field in the table structure called "date" and use the "CURRENT_TIMESTAMP" as default value, this will auto-populate the date/time on the record upon insert...
Order by this field DESC, limit x
Also, I have experienced many cases of the wrong data appearing thanks to grouping... Make sure your data is correct before ORDER BY and LIMIT is applied
For getting posts from user1 to user1 there's no need to group by:
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC LIMIT 3
For getting posts from * to user1:
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC LIMIT 3
For getting posts from * to user1, only unique users:
SELECT * FROM posts
WHERE toID="USER1_ID"
GROUP BY FromID
ORDER BY date DESC LIMIT 3
Somtimes you will run into the problem where GROUPED records are not ordered by ORDER BY, because the ORDER BY is applied to the result AFTER the grouping is applied... To achieve a workaround:
SELECT * FROM (
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
To Get the last 3 users who have most recently sent themselves a post:
SELECT * FROM (
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3

try this query.
$query = mysql_query("SELECT * FROM posts WHERE toID=fromID GROUP BY id ORDER BY date DESC LIMIT 3");

Related

select mysql from a row limit and asc php pdo

I have a table like below
ID
1
2
3
4
5
I want to select ID 2 then ASC LIMIT 3. I want to get 2,3,4.
My select goes.
SELECT * FROM TABLEID WHERE ID = 2 AND status = 'unuse' ORDER BY ID ASC LIMIT 3
But I only get 1 record I am expecting 3 row to be returned base on the LIMIT 3
I am expecting 3 row to be returned base on the LIMIT 3
You are expecting wrong, because LIMIT can not create records that aren’t there to begin with. You have only one record with ID=2, so a WHERE clause selecting those records of course only returns this one.
You want WHERE ID >= 2 to first select all records that have an id 2 or greater, and then limit that selection to 3 records only.
select * from TABLED limit 1,4
1 is offset start point
4 is upto count point
SELECT * FROM TABLEID WHERE ID BETWEEN 2 AND 4
OR exclude the id
SELECT * FROM TABLEID WHERE ID >=2 status = 'unuse' AND id <> 1 ORDER BY ID ASC LIMIT 3

selecting last 10 distinct rows with subqueries

I want to select the last 10 rows from my messages table that have either the receiver_id = 1 or the sender_id = 1 but those which have the receiver_id = 1 have DISTICT applied on the sender_id and those which have the sender_id = 1 have the DISTINCT property applied on the receiver_id.
Basically, what am I trying to say is that I want to select the very last 10 messages either sent either received by the user with the id of 1, sent to or sent by 10 different other users.
What have i tried so far:
SELECT * FROM (SELECT DISTINCT sender_id FROM messages WHERE receiver_id = 1) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
SELECT * FROM (SELECT * DISTINCT receiver_id FROM messages WHERE sender_id= 1) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
But trying to get the separately and adding the arrays (and sorting them by the ids of the messages) didn't quite work.
So I tried making a query like so:
SELECT * FROM (SELECT DISTINCT receiver_id, sender_id FROM messages WHERE (receiver_id = 1 OR sender_id = 1)) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
but it turned out my query is a total mess and, of corse, doesn't return me what I wanted.
P.S.: I am using XAMPP MySQL: MariaDB
Your second paragraph is:
Basically, what am I trying to say is that I want to select the 10
very last messages (rows) stored in the database that were either sent
or received by the user with the id of 1.
This is a fairly basic query:
SELECT m.*
FROM messages m
WHERE 1 IN (m.receiver_id, m.sender_id)
ORDER BY m.id DESC
LIMIT 10;

select query from 2 tables with conditions

I have page where people can post comments and page where people can click "follow" on other people profiles (same as LIKE on Facebook)
I would like to have SELECT query that will post all the comments I have, but will order them with the follow way:
First, print the 2 newest comments (they must been posted this week) of the lastest people you click FOLLOW.
Second, post the rest of the posted, order them by create-date
(I'm using linux time)
Can you help me with the SQL query?
This is my current SELECT query. it pull all comment by create-date:
SELECT
id, userID, text, createDate
FROM `comments` AS comment
WHERE (comment.refID = 0)
AND (comment.pageName = 'yard')
AND 1=1
ORDER BY comment.createDate DESC LIMIT 0, 20
"followers" table looks like this:
userID ownerID createDate
1 2 1439019657
1 4 1438940399
(user 1 is following after user 2 and 4)
"comments" table looks loke this:
id userID pageName refID text createDate
220 1 yard 0 text1 1438030967
227 1 yard 0 text2 1438031704
228 1 yard 0 text3 1438031704
(userID - which user publish the comment. refID - always "0". pageName - always "yard")
So is this case, if I'm user number 1, than I would like to see the newest 2 comments of users 2 and 4 (only if they where made in the last week) and than to see all the rest of the comments (of all users) order by date (without , of course, the once that I already saw)
You are going to have to split this up into 2 queries.. ..and I am guessing at some of the table relationships here..
// get the last 2 comments from followed users..
SELECT *
FROM comments
WHERE userID IN (
SELECT ownerID
FROM followers
WHERE userID = ?
) ORDER BY createDate DESC
LIMIT 2
// then get the rest, removing already returned comments
SELECT *
FROM comments
WHERE id NOT IN (
-- either put the above query in here
-- or buuild an array of the 'id's when outputting the results above
-- and use that in here to prevent them being returned again
) ORDER BY createDate DESC
(select com.userID,com.page,com.text
from followers as fol
JOIN comments as com
ON fol.ownerId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
Order BY com.createDate desc
)
Union
(select com.userID,com.page,com.text
from comments as com
where com.id NOT In
(select com.id
from followers as fol
JOIN comments as com
ON fol.userId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
)
Order BY com.createDate desc
)
Explanation:
There are two queries merged in one using union.
Select statement will give all the data follower comments.
this is accomplished using Join.
Select statement to get all the other comments then the comments of the above 1st query.
the only thing that is not clear is how you are deciding whether the
comments is been read by the user (Insufficient information)

MySQL query multiple column ordering

So, I've written a query which should grab 15 most recent results from the 'messages' table, but order results by date in descending direction. My current query is as follows:
SELECT * FROM messages
WHERE chatID = 1
ORDER BY ID DESC, timeSent ASC
LIMIT 15
As you can see, I am using the 'ID DESC' to get the 15 most recent results, but the 'timeSent ASC' isn't ordering the results in the order I wish.
How can I correct my query to achieve this?
First fetch the messages by ordering the ID then sort it according to timeSent. You can try this -
SELECT * FROM
(SELECT * FROM messages WHERE chatID = 1 ORDER BY ID DESC LIMIT 15) messages_ordered
ORDER BY timeSent ASC

PHP / MySQL - Getting last 3 uniquely updated records

Im wondering if someone could help out.
I need to write a query that gets the last 3 'created' records, but their UID has to be unique so for example, my mysql fields look like so
uid created
19 2012-02-01 01:08:43
18 2012-02-31 17:07:21
19 2012-02-31 16:07:20
20 2012-02-31 13:03:00
Ok, so i want to get the last 3 uid's created ... but they have to be unique uid's so the same uid cant appear twice.
Cheers
this should do this
SELECT * FROM ( SELECT * FROM tablename ORDER BY uid, created DESC ) ordered GROUP BY uid
You can select last 3 IDs with this query:
SELECT * FROM ( SELECT * FROM table ORDER BY uid, created DESC ) AS selected GROUP BY uid LIMIT 3
Logic is: order table by uid field in descending and limit to 3 fields.
Use a Distinct and Group by to get what you're after:
SELECT DISTINCT * FROM table GROUP BY uid;
That will give you all unique UID's.
Then add your ORDER BY in there to make sure you grab the last records.
SELECT *
FROM table
ORDER BY created DESC
GROUP BY uid LIMIT 0,3
SELECT uid
, MAX(created) AS max_created
FROM tableX
GROUP BY uid
ORDER BY max_created DESC
LIMIT 3

Categories