sql query to select the latest entry of each user - mysql

I have a location table in my database which contains location data of all the users of my system.
The table design is something like
id| user_id| longitude| latitude| created_at|
Now I have a array of user ids and I want to write a sql query to select the latest location of all these users. Can you please help me with this sql query ?

In the user_id in (......) at the end of the query you sould insert your array of user ..
select * from my_table
where (user_id , created_at) in (select user_id, max(created_at)
from my_table
group by user_id)
and user_id in ('user1','user2',... ) ;

SELECT
t1.ID,
t1.user_id,
t1.longitude,
t1.latitude,
t1.created_at
FROM
YourTable t1
INNER JOIN
(SELECT MAX(id), user_id FROM YourTable GROUP BY user_id) t2 on t2.user_id = t1.user_id
INNER JOIN
yourArrayTable ON
yourArrayTable.user_id = t1.user_id

Related

select join two table then order by latest upload time(uplaod_time column) in mysql select query

I got two table users(table 01)、record_dcm_upload(table02)
i try to query counts and latest upload file time by everylogin account(users.username)
like
SELECT record_dcm_upload.user_id, users.username, record_dcm_upload.upload_time, COUNT( * )
FROM record_dcm_upload
JOIN users ON ( users.id = record_dcm_upload.user_id )
GROUP BY record_dcm_upload.user_id
but my query sql got some problem (actually the result of upload_time not the latest)
how should i adjust my query code (hope user_id and upload_time all sort By DESC)
SELECT t2.id, t2.username, MAX(t1.upload_time), COUNT(*)
FROM record_dcm_upload t1
JOIN users t2 ON ( t2.id = t1.user_id )
GROUP BY t2.id, t2.username

Select from mysql table unique records according to a Field

I have a table in MySQL which I store registrations info.
I need to select all rows but only the latest according to email field and ID field
My table:
and I need
select *
from yourTablename
where (id,email) in
(select max(id),email from yourTablename group by email);
Another approach with Inner Join:(As suggested by Tim)
SELECT t1.*
FROM t t1
INNER JOIN
(
SELECT email, MAX(id) AS max_id
FROM t t2
GROUP BY email
) t2
ON t1.email = t2.email AND
t1.id = t2.max_id;
DEMO
Thanks for the replies.
Using
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT email, MAX(name) AS max_name
FROM yourTable
GROUP BY email
) t2
ON t1.email = t2.email AND
t1.name = t2.max_name;
Worked fine.
Thanks

SQL count number of common matches using several WHERE clauses

I have a table having columns like: membership_id | user_id | group_id
I'm looking for a SQL query to get the number of common groups between 2 different users. I could do that in several queries and using some PHP but I'd like to know if there is a way to use only SQL for that.
Like with the user ids 1 and 3, there are 3 common groups (1, 5 and 6) so the result returned would be 3.
I've made several tests but so far no result...Thank you.
You don't need "multiple WHERE clauses" or even a self JOIN:
SELECT group_id
FROM theTable AS t
WHERE t.user_id IN (1, 3)
GROUP BY group_id
HAVING COUNT(DISTINCT user_id) = 2;
more generically
SELECT group_id
FROM theTable AS t
WHERE t.user_id IN ([user id list])
GROUP BY group_id
HAVING COUNT(DISTINCT user_id) = [# of user ids in list];
Edit: Oh, you wanted the number of groups....
SELECT COUNT(1) FROM (
SELECT group_id
FROM theTable AS t
WHERE t.user_id IN (1, 3)
GROUP BY group_id
HAVING COUNT(DISTINCT user_id) = 2
);
You can achieve this with join.
Try this:
select t1.user_id, t2.user_id, group_concat(distinct t1.group_id)
from your_table t1
join your_table t2
on t1.user_id < t2.user_id
and t1.group_id = t2.group_id
group by t1.user_id, t2.user_id;
If you don't want a concatenated output:
select distinct t1.user_id, t2.user_id, t1.group_id
from your_table t1
join your_table t2
on t1.user_id < t2.user_id
and t1.group_id = t2.group_id;
Try to join two instances of the same table (for each of them you select only the records relative to one of the users) using group_id as join attribute, and count the result:
SELECT COUNT(*)
FROM table AS t1
JOIN table AS t2 ON t1.group_id=t2.group_id
WHERE t1.user_id=1 AND t2.user_id=3;
SELECT COUNT(*)
FROM TABLE_NAME USER_ONE_INFO
TABLE_NAME USER_TWO_INFO
WHERE USER_ONE_INFO.ID = USER_ONE_ID
AND USER_TWO_INFO.ID = USER_TWO_ID
AND USER_ONE_INFO.GROUP_ID = USER_TWO_INFO.GROUP_ID;

Need to convert raw queries into laravel Eloquent

Below is my raw MySQL query:
SELECT T1.id, T1.department_id, COALESCE(T2.cnt, 0) AS cnt
FROM (SELECT DISTINCT id, department_id FROM users WHERE company_id = '33') AS T1
LEFT JOIN (
SELECT user_id, COUNT(*) AS cnt
FROM userlogs
WHERE created_at BETWEEN '2015-12-17' AND '2016-01-12'
GROUP BY user_id
) AS T2 ON T2.user_id = t1.id
Can any one help me to convert it to Eloquent? I am new to Laravel.
I solved it using DB::query() which is pretty straight forward

using outer alias in mysql subquery

I am writing a mysql query and I have a question. Can I / How do I do something like this:
select rating, user_id, (
-- in here I want to write a subquery to get the number of times the user_id in the outter query has rated this teacher
) as user_rated_frequency from teachers_rating where teacher_id = id
Essentially I am trying to get data and the frequency in which that user rated that teacher. Is it possible to use an alias from one of the items I want to select in a subquery that is still in the select and not in the where clause?
Check out this ...
SELECT rating,
user_id,
(SELECT COUNT(*)
FROM teachers_rating t1
WHERE teacher_id = 3
AND t1.user_id = t2.user_id) AS user_rated_frequency
FROM teachers_rating t2
WHERE teacher_id = 3;
or that one:
SELECT AVG (rating) AS average_rating,
user_id,
(SELECT Count(*)
FROM teachers_rating t1
WHERE teacher_id = 3
AND t1.user_id = t2.user_id) AS user_rated_frequency
FROM teachers_rating t2
WHERE teacher_id = 3
GROUP BY user_rated_frequency;
Links above show a SQL Fiddle example assuming that id is 3.
Alternatively you could have a sub query in the FROM clause:
SELECT AVG (t1.rating),
t1.user_id,
t2.user_rated_frequency
FROM teachers_rating t1,
(SELECT tr.teacher_id,
tr.user_id,
COUNT(*) AS user_rated_frequency
FROM teachers_rating tr
GROUP BY tr.teacher_id) t2
WHERE t1.teacher_id = t2.teacher_id
AND t1.user_id = t2.user_id
GROUP BY user_id, user_rated_frequency
Hat a look at this Fiddle.
You need to move your subquery (technically called a derived table) into your from clause.
Something like so:
select
rating,
user_id,
from teachers_rating,
(in here I want to write a subquery to get the number of times the user_id in the outter query has rated this teacher) as user_rated_frequency f
where teacher_id = f.id