Select most recent entries and join with another table - mysql

My sql's rusty. I need to select the 30 most recent messages (those with the 30 largest id's), and then use the sender_id to join with the users table id.
In English, I want the users who sent the last 30 messages.
Here my query (which doesn't run):
SELECT * FROM group_messages
WHERE group_id=52
ORDER BY id DESC
LIMIT 30
LEFT JOIN users
ON users.id=group_messages.sender_id
If there's a better approach to this kind of query, then by all means.
Note: The first part works in selecting the 30 most recent messages. The trouble came when I tried joining on the users table. (And I just realized even if this query did run, I would need to add GROUP BY users.id a user may have sent more than 1 of the 30 most recent messages.

The JOIN clause has to come before WHERE, ORDER BY, and LIMIT
SELECT DISTINCT id
FROM (
SELECT u.id
FROM group_messages AS g
INNER JOIN users AS u on u.id = g.sender_id
WHERE group_id = 52
ORDER BY id DESC
LIMIT 30) AS x
I put it in a subquery so I could then perform the DISTINCT selection. If you do that in the same query, it will get rid of the duplicates before selecting the most recent 30 rows, so you'll get the 30 most recent senders, not the senders of the 30 most recent messages.
I doubt you really need a LEFT JOIN instead of INNER JOIN. That would only be needed if a message could have a sender ID that isn't in users.

Related

MySQL GROUP BY w/ ORDER BY not having desired result

I have a query that attempts to retrieve IDs of people, but only if those people have more than one address. I'm also checking that the last time I called them was at least 30 days ago. Finally, I'm trying to order the results, because I want to pull up results with the oldest last_called datetime:
SELECT
p.id,
COUNT(*) AS cnt
FROM
people p
LEFT JOIN addresses a
ON p.id = a.id
WHERE p.last_called <= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY p.id
HAVING COUNT(*) > 1
ORDER BY p.last_called ASC
LIMIT 25
Right now, the results are not excluding people with only one address. I haven't even got to the point where I know if the sort order is correct, but right now I'd just like to know why it is that my query isn't pulling up results where there is at least 2 addresses for the person.
If you don't want to include people with no address then I would recommend using INNER JOIN instead of LEFT JOIN and DISTINCT to get distinct address ids
(just in case if you have duplicate mappings), e.g.:
SELECT
p.id,
COUNT(DISTINCT(a.id)) AS cnt
FROM
people p
JOIN addresses a
ON p.id= a.peopleid
WHERE p.last_called <= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY p.id
HAVING COUNT(DISTINCT(a.id)) > 1
As far as Ordering is concerned, MySQL evaluates GROUP BY before ordering the results and hence, you need to wrap the query inside another query to get the ordered results.
Update
Instead of joining on aid, you need to join on peopleId of an address record to get the people record.

left join query does not return left table records if right table does not have records

I want to output users and their total number of wins and losses over requested date interval. When I run the query below within a date range that contains records in results table, everything works fine. However if a user does not have any records in results table for the requested date interval, then no user returned in the request at all. What I want is to return a user record anyway, even if the user does not have any records in results table for the requested date interval.
I believe the GROUP BY makes it behave that way, but I'm not sure how to configure the query to work the way I need it to.
SELECT
users.name,
users.division,
SUM(results.wins) as wins,
SUM(results.losses) as losses
FROM users LEFT JOIN results ON users.user_id = results.user_id
AND results.date BETWEEN {$startDate} AND {$endDate}
WHERE users.user_id = {$user_id} GROUP BY results.user_id;
The user is returned, just on a row where the id is NULL. You are grouping by the id in second table.
Instead, group by the first table field:
SELECT u.name, u.division,
SUM(r.wins) as wins, SUM(r.losses) as losses
FROM users u LEFT JOIN
results r
ON u.user_id = r.user_id AND r.date BETWEEN {$startDate} AND {$endDate}
WHERE u.user_id = {$user_id}
GROUP BY u.user_id;
---------^

joining two tables based on recent logged in datetime

I am working on MySql and have two tables login_users and login_timestamps
Table login_users keeps a record of user_id, name and address whereas table login_timestamps keeps a record of user_id and timestamp, this table adds a new entry each time user logs in, so for example if the user_id '1' logs in 10 times a day, this table will have 10 entries for user_id '1' for today.
Now I need to fetch user profiles based on their last logged in time.
for example if there are 3 users, the MySql query should give me 3 records with their latest logged in time.
The query I am using is
SELECT * FROM login_users LEFT JOIN login_timestamps ON login_users.user_id = login_timestamps.user_id ORDER BY login_timestamps.timestamp DESC
but this gives me all the previous logged in entries rather than the recent one only.
Of course you will get all logged in entries while you didnt specify when or what day or something , hete you need a where clause.
try that:
SELECT * FROM login_users
LEFT JOIN login_timestamps ON login_users.user_id = login_timestamps.user_id
WHERE DATE(`timestamp`) = CURDATE()
ORDER BY login_timestamps.timestamp DESC
this will give you entries for curent day. of course you can specify any condition you want.
EDIT: from your comment.
try that
SELECT l.user_id, max(timestamp) as lasttime
FROM login_users l
LEFT JOIN login_timestamps lt ON l.user_id = lt.user_id
GROUP BY l.user_id
ORDER BY lasttime DESC

sql nested inner join only returning 1 result

I'm building a Chatapplication that's a bit like the facebookchat. I have users,conversations and messages. All 3 have their own tables. For now I try to get all converstations containing a certain user and the latest message of the conversation.
I tried this query, but in a fact I only get 1 row back, but there are more rows matching
SELECT conversations.id as converid,
messages.from as messageauthor,
messages.message as message
FROM conversations INNER JOIN (SELECT * FROM messages
ORDER BY date DESC LIMIT 1) as messages
ON messages.conversationid=conversations.id
WHERE user1=3
OR user2=3
When I do i.e.
SELECT conversations.id as converid,
messages.from as messageauthor
FROM conversations INNER JOIN messages
ON messages.conversationid=conversations.id
WHERE user1=3
OR user2=3
I get all results, for sure, and when I check the converid's I get 3 unique Id's, so at least there are 3 converstations going on with userid 3. So the top query should also return 3. Now I don't understand why it only returns 1 row. Does the limit 1 from the nested query affect the whole query?
Looking forward for some pointers...
No. The limit 1 affects the subquery, so it is only returning one row. So, there is only one match.
What is the issue with this query (your second query, but formatted differently):
SELECT c.id as converid, m.from as messageauthor
FROM conversations c INNER JOIN
messages m
ON m.conversationid=c.id
WHERE user1=3 OR user2=3;
I see, you want the latest message. Try calculating it and joining back in:
SELECT c.id as converid, m.from as messageauthor
FROM conversations c INNER JOIN
messages m
ON m.conversationid=c.id join
(select m.conversationid, max(date) as maxdate
from messages m
group by m.conversationid
) mmax
on mmax.conversationid = m.conversationid and m.date = mmax.maxdate
WHERE user1=3 OR user2=3;

MySQL query performance problem - INNER JOIN, ORDER BY, DESC

I have got this query:
SELECT
t.type_id, t.product_id, u.account_id, t.name, u.username
FROM
types AS t
INNER JOIN
( SELECT user_id, username, account_id
FROM users WHERE account_id=$account_id ) AS u
ON
t.user_id = u.user_id
ORDER BY
t.type_id DESC
1st question:
It takes around 30seconds to do this at the moment with only 18k records in types table.
The only indexes at the moment are only a primary indexes with just id.
Would the long time be caused by a lack of more indexes? Or would it be more to do with the structure of this query?
2nd question:
How can I add the LIMIT so I only get 100 records with the highest type_id?
Without changing the results, I think it is a 100 times faster if you don't make a sub-select of your users table. It is not needed at all in this case.
You can just add LIMIT 100 to get only the first 100 results (or less if there aren't a 100).
SELECT SQL_CALC_FOUND_ROWS /* Calculate the total number of rows, without the LIMIT */
t.type_id, t.product_id, u.account_id, t.name, u.username
FROM
types t
INNER JOIN users u ON u.user_id = t.user_id
WHERE
u.account_id = $account_id
ORDER BY
t.type_id DESC
LIMIT 1
Then, execute a second query to get the total number of rows that is calculated.
SELECT FOUND_ROWS()
That sub select on MySQL is going to slow down your query. I'm assuming that this
SELECT user_id, username, account_id
FROM users WHERE account_id=$account_id
doesn't return many rows at all. If that's the case then the sub select alone won't explain the delay you're seeing.
Try throwing an index on user_id in your types table. Without it, you're doing a full table scan of 18k records for each record returned by that sub select.
Inner join the users table and add that index and I bet you see a huge increase in speed.