SQL for message inbox [duplicate] - mysql

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have an inbox of messages for my users, I have a 'messages' table and a 'users' table. Messages table has to and from fields which contain the user IDs.
I want to select the latest message from every user, where the to field is the current user ID, i.e.
"select (latest Message, by Message.ID) from (unique users) where Message.to = $currentUserID (and left join User where UserID = Message.from)"
I want to end up with something like this:
http://www.innerfence.com/blog/wp-content/uploads/screenshot-iphone-inbox-thumb.png
I can't figure out the query I need for this, please help..!

Try this. It is tested and working fine. Updated to include information about the sender.
SELECT * FROM `messages` tm LEFT JOIN `users` tu ON `tm.from` = `tu.userid`
WHERE `tm.date` IN
(SELECT MAX(`date`) FROM `messages` WHERE `to` = $currentUserID GROUP BY `from`);

Related

Update table with values from another table - mysql [duplicate]

This question already has an answer here:
Insert into one table base on join result from 2 other table
(1 answer)
Closed 3 years ago.
I have an account and customer tables which can be linked by customer col and get the date col to the account table.
account table:
customer table:
final table:
If I use the below query, I am getting following error
ERROR: more than one row returned by a subquery used as an expression
What am I doing wrong?
UPDATE account
SET date = (
SELECT date
FROM customer
WHERE customer.customer = account.customer
);
Consider the update ... join ... set syntax:
update account a
inner join customer c on a.customer = c.custom
set a.date = c.date

Can't update existing records MySQL [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 4 years ago.
I've created a new column (third one) and all records are null in that column. The table has 100 rows. When I try INSERT INTO, records are added but only from 101 row and previous ones are still null. That's why I decided to use UPDATE, but I have the next error message: You can't specify target table 'actors' for update in FROM clause
Here is my query:
UPDATE
actors
SET
starred_count = (
SELECT COUNT(actors.Actor_id)
FROM starred
INNER JOIN actors ON actors.Actor_id = starred.Actor_id
GROUP BY starred.Actor_id
ORDER BY starred.Actor_id
)
How could I do this properly?
Give a try to next query, since on MySQL updating using a query is not valid.
UPDATE
actors AS a
INNER JOIN
(SELECT Actor_id, COUNT(*) AS starred_count
FROM starred
GROUP BY Actor_id) AS s ON s.Actor_id = a.Actor_id
SET
a.starred_count = s.starred_count

Is it possible to limit a row in MySQL?

The problem is that when I echo user information, it also echos a column that multiple times even though it has one value. I want to echo out the maximum value of that column instead of doing it multiple times depending on other columns.
$sql = "SELECT * FROM users JOIN user_images ON users.username = user_images.username WHERE users.username = '$username' GROUP BY user_images.username";
The users table have one column with one value like a profile picture. The user_images table have one column with multiple values like posts. I also researched that GROUP BY should help with this problem, but it seems to be not working. Is there a way to just echo the maximum of rows or limit the other one?
You need to replace the * in the SELECT * bit of your query so that you are only selecting one value per column that is not in your GROUP BY clause.
For example:
SELECT user_images.username, MAX(user_images.posts)
FROM users
JOIN user_images
ON users.username = user_images.username
WHERE users.username = '$username'
GROUP BY user_images.username";
Will return each username and the max of the posts column corresponding to that user. You can add more columns to your SELECT statement as long as you're only returning one record per your GROUP BY
variable.
To return two tables for your desired outcome:
One table which is one column of posts for the specified user:
SELECT posts
FROM user_images
WHERE username = '$username';
And a second one for that username and profile image:
SELECT username, profile_image
FROM user_images
WHERE username = '$username'
GROUP BY username, profile_image;
You don't need the joins you were doing if all of these columns are in the user_images table. If you also want columns from the users table you can add the join back in.

MySQL Returning Incorrect Values using Group Bu [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I am trying to select the most recent RowID from an activities table for each user in an Account. But everytime i run the query, it returns the correct RowID but the other information seems to be selected at random as the Subject and Date are always from an earlier RowID.
Why does MySQL select the correct most recent RowID but then return random values for the the SubjectDate etc..
`
SELECT
MAX(activities.rowid) as RowID,
contacts.firstname as First,
contacts.lastname as Last,
activities.visiondescription as Subject,
smsreceived as Date
FROM activities, contacts
WHERE activities.contactid=contacts.contactid
AND activities.accountid=contacts.accountid
AND activities.accountid = 'AAXA-S0BJ7I'
group by activities.RowID;
Anyone see what i might be doing wrong?
I have tried to use group by activities.ContactID, activities.SMSReceived and still no joy.
Thanks
Try something like this:
SELECT contacts.contact_id,
contacts.firstname as First,
contacts.lastname as Last,
GROUP_CONCAT(DISTINCT IF(activities.rowid = MAX(activities.rowid),visiondescription,NULL)) AS Subject
GROUP_CONCAT(DISTINCT IF(activities.rowid = MAX(activities.rowid),smsreceived,NULL)) AS `Date`
FROM activities, contacts
WHERE activities.contactid=contacts.contactid
AND activities.accountid=contacts.accountid
AND activities.accountid = 'AAXA-S0BJ7I'
GROUP BY contacts.contact_id;
The idea here is that the needed information pertains to the user and so the GROUP BY needs to be against the user. The GROUP_CONCAT will pick out only that row among the grouped rows that meeds the IF condition.
-- just a thought

mysql: two tables but three select criteria

I have two tables: users and user_depts. Let's say (for this question) that users only has an 'id' column, and user_depts has 2: 'uid' and 'did'.
I am looking for an SQL line that will return all the user IDs for all the departments with which a given user ID (let's say 7, though this'll come dynamically from PHP) is associated.
What I've tried is:
SELECT id FROM users, user_depts
WHERE users.id = user_depts.uid
AND user_depts.uid = 7
But of course this does nothing but return 7. I think I might have to join the table to itself, but I only know the shortcut syntax for joining, and it doesn't seem to be sufficient. Any pointers would be greatly appreciated!
Use EXISTS:
SELECT uid FROM user_depts
WHERE EXISTS (
SELECT * FROM user_depts a
WHERE uid = 7 AND a.did = user_depts.did
)
I am looking for an SQL line that will return all the user IDs for all
the departments with which a given user ID (let's say 7, though
this'll come dynamically from PHP) is associated.
If this means you want to find all the users with the userid: 7 that has a user_departent connected to it this is your query:
select users.id from users
inner join user_depts.uid = users.id
where users.id = 7
select uid from user_depts where did
in (select did from user_depts where uid=7)
First select all the did with which a user is associated using subquery
Then select all the user_id for the selected departments.
It's the best way you can do it.
and if you want to remove repeated result then you can use distinct