Aggregate MYSQL functions getting a value in the same row - php

$query = mysql_query("SELECT *, MAX(date_time) FROM messages
WHERE user_to='$current_user' OR user_from='$current_user'
GROUP BY conversation
ORDER BY date_time DESC LIMIT 0,5") or die(mysql_error());
echo $message['MAX(date_time)'];
How can I display the column 'content' that is in the same row as the date_time being selected here, so the time matches with the message? Would it be something like
echo $message['MAX(content)'];
I am trying to have 'messages' in groups (called conversations) and I want to display the most recent message. It is currently displaying the most recent time but not the most recent message.
Thanks.

If your table has an autoincrement id column to use as the message id, then the highest id value should also be the latest message in the conversation

Try Below:
SELECT * from messages as ts
LEFT JOIN (select max(id) as maxid from messages group by date_time) as tsm
ON ts.id=tsm.maxid
WHERE (ts.user_to='$current_user' OR ts.user_from='$current_user')
ORDER BY ts.date_time DESC LIMIT 0,5
Assuming id is primary key column in your message table

Order your messages by date_time and only take the first tuple (LIMIT 1).
I don't get why you aggregate if you just want the latest tuple from messages. Perhaps you can elaborate this and provide some more information about the table.
SELECT content, date_time
FROM messages
WHERE user_to='$current_user' OR user_from='$current_user'
ORDER BY date_time DESC
LIMIT 1

If you have auto incrementing key, you can try:
$query = mysql_query("SELECT * FROM messages as m
WHERE user_to='$current_user' OR user_from='$current_user' AND
id IN (SELECT MAX(id) FROM messages as m2 GROUP BY m2.conversation)
ORDER BY date_time DESC LIMIT 0,5") or die(mysql_error());

Related

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;

Get total points of user in MySQL Table with PHP

I have a MySQL table with the columns "user" and "warningpoints" for each warning as you can see in the table above. How can I get the user which has the most warningpoints in total in PHP?
You can use GROUP BY and ORDER BY and LIMIT:
SELECT t.user,sum(t.warningPoints) as sum_points
FROM YourTable t
GROUP BY t.user
ORDER BY sum_points DESC
LIMIT 1;
Or if there is only one record per person, no need to group :
SELECT t.user,t.warningPoints
FROM YourTable t
ORDER BY t.warningPoints DESC
LIMIT 1;
You can simply add below statements in your php code.
$sql = "SELECT User, Max(warningpoints) AS MaxWarningpoints FROM MyGuests GROUP BY User";
$result = $conn->query($sql);

display only last message from each user

Hello friends i want to display the last message from each user in this table, lets assume the $_SESSION['id'] is 1 so i want to display the last messages by each from or to user 1 here is my query:
SELECT * FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC
but when i run this it displays two messages from a user that is in the msg_from column and also in the msg_to column and it doesn't display the last inserted message, please guys, I need help.
since you are ordering by MAX(msg_id) DESC then you reqire only the first result because it will be the latest then you may do something like
SELECT TOP 1 * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC);
or
SELECT * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC) LIMIT 1;
hope it helps :)
Fetching the last messages for user-1 from others:
SELECT * FROM message WHERE msg_to = '1'
GROUP BY msg_from ORDER BY msg_date DESC
Fetching the last messages from user-1 to others:
SELECT * FROM message WHERE msg_from = '1'
GROUP BY msg_to ORDER BY msg_date DESC
I didn't use your MAX(msg_id) for ordering the data, instead, I used the date to get the latest messages.

How can I order by count in mysql when the count need data to calculate from this select statement?

Look at my code, I want the select statement order by the count percentage after I fetch the data from this select statement, obviously, it's not logical. What can I do? Help, appreciate.
<?php
//myslq connection code, remove it because it's not relate to this question
$stm =$db->prepare("SELECT id ,term_count, COUNT(user_id) as count FROM sign WHERE term IN (:term_0,:term_1) GROUP BY user_id ORDER by count DESC");
//trying replace order by count with $combine_count, but it's wrong
$term_0="$term[0]";
$term_1="$term[1]";
$stm->bindParam(":term_0", $term_0);
$stm->bindParam(":term_1", $term_1);
stm->execute();
$rows = $stm->fetchALL(PDO::FETCH_ASSOC);
foreach ($rows as rows) {
$count=$rows['count'];
$term_count_number=$rows['term_count'];
$count_percentage=round(($count/$count_user_diff)*100);
$count_key_match=round(($count/$term_count_number)*100);
$combine_count=round(($count_percentage+$count_key_match)/2);
//issue is here, I want the select statement order by $combine_count
}
?>
SELECT id ,term_count, COUNT(user_id) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
Since "count" is a function, it would be better to put backtics around the non-function "counts", as done above.
GROUP BY should list the field not aggregated. Otherwise, it does not know which id and term_count to fetch. So, depending on what you are looking for,
Either do
SELECT user_id, COUNT(*) as `count` -- I changed this line
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
or do
SELECT id ,term_count, COUNT(*) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY id ,term_count -- I changed this line
ORDER by `count` DESC");
SQL Syntax Logic
SELECT column1, count(column1) AS amount
FROM table_name
GROUP BY column1
ORDER BY amount DESC
LIMIT 12

select query to get all values and also distinct value

Hi i am trying to get distinct values and some other data from same table A . the code i tried is
$query2="select DISTINCT(from_id) from messages where to_id='$userid' order by messagedate DESC";
$res2=mysql_query($query2);
while($row2=mysql_fetch_row($res2))
{
$query="select * from messages where to_id='$userid' and from_id='$row2[0]' ORDER BY messagedate DESC"
using the above method i am unable to filter distinct values hence i tried like this
select msgid,DISTINCT(from_id) from messages where to_id='21' order by messagedate DESC
Its an error. need help pls
Try like this
select DISTINCT(from_id),msgid from messages where to_id='21' order by from_id DESC
Look into the GROUP BY statement
I think you need something like
SELECT msgid, from_id
FROM messages
WHERE to_id = 21
GROUP BY from_id
ORDER BY messagedata DESC
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

Categories