user_id | group_id
------------------
5 3
6 1
6 3
7 1
7 2
8 2
My join table looks like this. The query I'm trying to accomplish is finding if two user_ids have a matching group_id.
I don't need any specific information back from the query, just a simple true or false.
Try this:
select
(select group_id from tbl where user_id = 'user_id1')
<=>
(select group_id from tbl where user_id = 'user_id2');
Related
I'm trying to count how many of the objects appear in table 2 without the status of 3 or 5. So if it has the status of 3 or 5 I want to exclude it from the count. Where I'm stuck is there are duplicate values, as they have may more than one status. Further explanation below.
Table 1
Object_ID
1
2
3
4
5
Table 2
ID | object_id | status
1 2 2
2 2 3
3 2 5
4 3 2
5 3 2
6 3 7
END GOAL
Count how many object_ids have a status excluding 3 or 5. But also to ignore duplicates. In this example, the total count would be 1 (with the object_id being 3). As I need to find all of the rows in table 2, then essentially merge them together assuming neither of them has a status of 3 or 5.
Count. | object_id
1 | 3
SELECT Count(distinct(object_id))
FORM table_2
WHERE status <> 3 or status <> 5
I seem to be able to group them if the status is 3 or 5, but I can't seem to exclude them.
Hopefully, it makes sense, I've tried to simplify it so I don't have irrelevant code included.
One method without subqueries is:
select count(distinct object_id) - count(distinct case when status in (3, 5) then object_id end)
from table_2;
This counts the number of distinct object ids and then subtracts the number of distinct object ids that have the specified statuses.
More typically, I would approach this with two levels of aggregation:
select count(*)
from (select object_id
from table_2
group by object_id
having sum( status in (3, 5) ) = 0
) o
You can use aggregation:
select count(*)
from (
select object_id
from table2
group by object_id
having max(status in (3, 5)) = 0
) t
I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)
i have 2 tables as following.
User
id name
---------------
1 john
2 raju
3 manu
4 raghu
friendtable
id userid recvId
------------------------
1 1 2
2 1 3
3 2 3
4 3 4
Is it possible to filter users by their friends count from these tables.Please help me.
For eg :- range >=3 will result : john,manu
range >3 and range <2 will result : raju
range <2 result : raghu
Do a UNION ALL to get all id's from friendstable in one column. Join users table with that result.
Do a GROUP BY, adjust HAVING to decide what to return, e.g. at least 3 times etc.
select u.name
from users
join (select userid as id from friendtable
union all
select recvId as id from friendtable) f
on u.id = f.id
group by u.name
having count(*) >= 3
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recvid>=3
SELECT name FROM user a,friendtable b WHERE a.id=b.id and b.recvid>3 AND b.recid<2
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recid<2
I'm trying to write a mysql query:
first select 5 rows and then get count with a where
first select 5 rows
table
id user_id
--------
1 1
2 2
3 3
4 1
5 1
6 4
7 3
8 1
id user_id
----------
1 1
2 2
3 3
4 1
5 1
And then get count this table where user_id =1
result = 3
You can try somthing like that
Select count(*) From
(Select * From T
order by ID asc Limit 5) as child
where user_id = 1
Looks like you want to present two different result sets together. You need to use a JOIN for this. Something like so will do the trick for you.
SELECT T.*,c.cnt
FROM T
JOIN ( SELECT COUNT(*) cnt FROM T where user_id = 1 ) c
LIMIT 5
The subquery generates your count as a one-row resultset, and the JOIN (which lacks an ON condition) puts it into every row of your other resultset.
If you wanted to show five rows from your table, and have each row mention the count for the userid in that row, you could do this.
SELECT T.*,c.cnt
FROM T
JOIN ( SELECT COUNT(*) cnt, user_id
FROM T
GROUP BY user_id
) c ON T.user_id = c.user_id
LIMIT 5
The way that summary (COUNT(), etc) queries and detail queries work together is a little intricate, but you will figure it out.
Beware, though: If you do a LIMIT without first doing an ORDER BY, MySQL is free to return any five rows it pleases.
I am a sql noob and not sure how to explain this. So here is what I'm trying to achieve:
Original table
id node_id tag_id
-------------------------
1 10 3
2 10 4
3 10 1
4 11 7
5 11 8
Trimmed table:
id node_id tag_id
-------------------------
1 10 3
4 11 7
It should be simple but I really left clueless, so appreciate your help.
It is unclear exactly what you want to do, but the following produces the output you specify:
select min(id), node_id, min(tag_id)
from table t
group by node_id;
EDIT:
If you want to delete rows, then use join:
delete o
from original_table ot left join
(select min(id) as id
from original_table
group by node_id
) tokeep
on ot.id = tokeep.id
where tokeep.id is null;
This uses a left join to match to the minimum id for each node_id. These are kept -- only non-matches are deleted.