SQL count number of common matches using several WHERE clauses - mysql

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;

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 the last record in each group along with count

We are having a following sqlite3 table named 'atable'
id student assignment grade
-----------------------------------
1 A 123 9
2 A 456 9
3 A 234 8
4 B 534 7
5 B 654 9
6 C 322 7
id is unique and incremented for each records. We are fetching latest assignment for each user by running query
SELECT student, assignment, grade from atable where id in
(select max(id) from atable group by student) order by id desc
This is working fine. However, we also need to fetch number of assignments for each user where user received a particular grade in the same query, say 9.
Any idea suggestion how to enhance or rewrite above query to return count as well. As mentioned, we are using sqlite3.
Thanks
You can use this correlated query:
SELECT t.student, t.assignment, t.grade,
(SELECT COUNT(*) FROM atable s
WHERE s.student = t.student and s.grade >= 9) as total_above_9
from atable t
where t.id in
(select max(id) from atable group by student)
order by t.id desc
It would be better to join to a derived table that contains an aggregated version of the original table:
select t1.student, t1.assignment, t1.grade, t2.cnt
from mytable as t1
join (
select student, max(id) as id,
count(case when grade = 9 then 1 end) as cnt
from mytable
group by student
) as t2 on t1.id = t2.id
Try this;)
select t1.student, t1.assignment, t1.grade, t2.count
from atable t1
inner join (select max(id) as id, count(if(grade=9, 1, null)) as count from atable group by student) t2
on t1.id = t2.id
order by t1.id desc

sql query to select the latest entry of each user

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

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

Returning the 'last' row of each 'group by' in MySQL

Is there a more efficient way of doing the following?
select *
from foo as a
where a.id = (select max(id) from foo where uid = a.uid group by uid)
group by uid;
)
This answer looks similar, but is this answer the best way of doing this - How to select the first row for each group in MySQL?
Thanks,
Chris.
P.S. the table looks like:
CREATE TABLE foo (
id INT(10) NOT NULL AUTO_INCREMENT,
uid INT(10) NOT NULL,
value VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
INDEX `uid` (`uid`)
)
data:
id, uid, value
1, 1, hello
2, 2, cheese
3, 2, pickle
4, 1, world
results:
id, uid, value
3, 2, pickle
4, 1, world
See http://www.barricane.com/2012/02/08/mysql-select-last-matching-row.html for more details.
Try this query -
SELECT t1.* FROM foo t1
JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
ON t1.id = t2.id AND t1.uid = t2.uid;
Then use EXPLAIN to analyze queries.
SELECT t1.* FROM foo t1
LEFT JOIN foo t2
ON t1.id < t2.id AND t1.uid = t2.uid
WHERE t2.id is NULL;
Returning the last row of each GROUP BY in MySQL with WHERE clause:
SELECT *
FROM foo
WHERE id IN (
SELECT Max(id)
FROM foo
WHERE value='XYZ'
GROUP BY u_id
)
LIMIT 0,30
if table is big in size. Make view containing all last row id
create view lastrecords as (select max(id) from foo where uid = a.uid group by uid)
Now join your main query with this view. It will be faster.
SELECT t1.* FROM tablename as t1
JOIN lastrecords as t2
ON t1.id = t2.id AND t1.uid = t2.uid;
OR You can do join with last records direct in query also:
SELECT t1.* FROM tablename as t1
JOIN (SELECT uid, MAX(id) id FROM tablename GROUP BY id) as t2
ON t1.id = t2.id AND t1.uid = t2.uid;
This has worked for me thanks!
SELECT t1.* FROM foo t1
JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
ON t1.id = t2.id AND t1.uid = t2.uid;
my version:
SELECT * FROM messages t1 JOIN (SELECT MAX(id) id FROM messages where uid = 8279 and actv=1 GROUP BY uid ) t2 ON t1.id = t2.id ORDER BY datex desc LIMIT 0,10;
this code below doesn't return all rows I want because if max id column is an inactive one then it skips the group even if the group has active rows..
select * from messages where uid = 8279 and actv=1 and id In (Select max(id) From messages Group By uid) order by datex desc;
The easiest way : you can select from selected list that is sorted.
SELECT * FROM (SELECT * FROM foo order by id DESC) AS footbl
group by uid
order by id DESC
This code works on me:
SELECT * FROM foo GROUP BY foo.uid
HAVING MAX(foo.id)