Select the max value from two tables - mysql

I have a query like this, to select the most recent time someone was contacted:
SELECT `user_id`, `last_contact`
FROM `emails_sent`
group by `user_id`
order by `last_contact` desc
The above code gives a table with the last contact time for each user. Now, I have another table with contacts to users, a table with columns user_id and last_contact, among others.
How can I make my select use both tables and select the last contact time for each user from the two tables?

Summarize the union of two summary queries, something like this.
SELECT user_id,
MAX(user_date) user_date
FROM
(
SELECT user_id,
MAX(last_contact) user_date
FROM emails_sent
GROUP BY user_id
UNION ALL
SELECT whatever_user_id_column user_id,
MAX(whatever_date_column) user_date
FROM whatever_table
GROUP BY user_id
)a
GROUP BY user_id

Related

combining 2 tables in mysql

Here is what I am looking to do but I have my doubts if its possible or not!
I have 2 tables, one is called users and the other is answers. I need to combine the results into a single statement, and order the table by time. the tricky part is that I have time on both tables and I need to take both time fields into account for ordering the results.
This is my unsuccessful attempt to come up with the query:
SELECT * FROM ( SELECT id, username, first_name, last_name, time FROM users
UNION ALL
SELECT id, answer, pid, uid, time FROM answers ) as AB
ORDER BY `AB.time`
UPDATE
I have come up with the following SQL Query:
SELECT username AS user, '' as page , '' as content , time FROM users
UNION ALL
SELECT (SELECT username FROM users WHERE `id`=`uid`) AS user, pid AS page, answer AS content, time FROM answers
ORDER BY `time` DESC
its what I am looking for but for some reason the user field in the second SELECT query is showing as null! any idea why?
SELECT id, username, first_name, last_name, time FROM users
UNION ALL
SELECT id, answer, pid, uid, time FROM answers
ORDER BY 'time`
solution to have different column name for each table
SELECT id AS t1_id,
username AS t1_username,
first_name AS t1_first_name,
last_name AS t1_last_name,
NULL ast2_id,
NULL AS t2_answer,
NULL AS t2_pid,
NULL AS t2_uid,
time
FROM users
UNION ALL
SELECT NULL AS t1_id,
NULL AS t1_username,
NULL AS t1_first_name,
NULL AS t1_last_name,
id ast2_id,
answer AS t2_answer,
pid AS t2_pid,
uid AS t2_uid,
time
FROM answers
ORDER BY time
If users.id and answers.uid are supposed to be the same then this should work:
SELECT u.*, a.* FROM users AS u
JOIN answers AS a ON a.uid = u.id
ORDER BY a.time

Mysql - getting distinct records

I have the following MySql table (user_actions) with the following columns:
id, user_id, created_at, action_type
I want a sql query that will get the latest actions that the user performed with no duplication of the actions.
for example:
user_id 1 has 3 records that has the action_type "follow"
and 2 records that has the action_type "unfollow"
in this case i want the query to return two records, one with action_type "follow" and one with "unfollow"
any thoughts?
You can use SQL's group by clause for this:
select user_id, action_type, max(created_at)
from user_actions
group by user_id, action_type
try this:
select ua.*
from user_actions ua
join (select max(id) as max_id,user_id,action_type from user_action group by user_id,user_action) ua_max
on ua.id=ua_max.max_id and ua.user_id=ua_max.user_id and ua.action_type=ua_max.action_type
Try this query:
select * from user_actions where action_type, created_at in
(select action_type, max(created_at) from user_actions group by action_type);

Help with a MySql Query - The Rows Holding the Group-wise Maximum of a Certain Column

I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id
what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.
I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right
select user_id, trans_date, trans_credit
from table
WHERE trans_credit =
(select max(trans_date) from inclick_account_act as f
where f.user_id = table.user_id);
Thanks in advance
If I understand you correctly you just want to get the most recent transaction for all users.
SELECT user_id, trans_date, trans_credit
FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;
How about something like
SELECT t.*
FROM table t INNER JOIN
(
SELECT user_id,
MAX(trans_date) max_trans_date
FROM table
GROUP BY user_id
) MaxDates ON t.user_id = MaxDates.max_trans_date

MySQL Distinct Records With Count

I have a photos table, with two columns in that table named "id" and "user_id". Obviously one user can have many photos. I'd like to run a query that can give me the photo count for each user.
Any help is appreciated.
select user_id, count(*) as photo_count from photos group by user_id
Use:
SELECT t.user_id,
COUNT(*) AS photo_count
FROM PHOTOS
GROUP BY t.user_id
use count as your aggregate function and groupby clause to give the count of specific attribute,
here is the sample:
SELECT user_id, count(*) 'Photos Count'
from photos
group by user_id

MySQL count elements in a database based on how many unique values there are in a field

(Sorry for the title, I don't really know how to phrase that :-) )
I have a table that has a date and a uid fields.
I need to get the number of uid for each date, currently I'm doing it in php running multiple queries like this one:
SELECT COUNT(uid) FROM users where Date = 'xxx';
Is there a simple way to achieve this with only an sql query?
Use the group by clause:
SELECT Date, COUNT(uid) FROM users group by Date;
To count the number of unique values use DISTINCT:
SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY `Date`
If your column is a datetime then you should use the DATE function to get the date part only:
SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY DATE(`Date`)
SELECT Date, COUNT(uid) FROM users GROUP BY Date