Say I have three tables that look like:
users:
id
event_registration:
id
user_id
event_id
events:
id
A user registers for an event and this registration is recorded in event_registration.
Is there a way I can calculate the count of events that a given set of users mutually share in a single MySQL statement? I figured out how to do this for two users, but I'd like to refactor
it to support X number of users (including just one user).
try
select count(event_id) as events
from event_registration
where user_id in (1,2,3)
group by event_id
having count(distinct user_id) = 3
You have to adjust the number 3 in the having clause acourding to the number of user_ids you are using in your in clause.
Related
I have a project, where customer needs a winnner pair for events. The users of this site can "like" other user's (just like on FB), they subscribe to a particular post, and the script will generate a winner pair from the subscribers.
So I need a SQL query to randomize a winner pair from the list of pairs, where the users liked each other, and subscribed to a particular post.
How do i do that ?
I cant write a query that, because i got unexpected results.
I have 3 tables : events, likes, subs (and users ofc)
events table : event_id, event_name
subs table: sub_id, event_id, uid
likes table: liker, liked (the two uid from the users table)
Now I can make pairs from the likes table (i self-joined the table where liker = liked AND liked = liker) and randomized, but how can I join the subs and the events tables to the likes table to achieve that a randomized pair will be a subscribed users for a particular event too ?
My current query looks like this :
SELECT L.liked AS T1, L.liker AS T2
FROM likes AS L, likes AS K
WHERE L.liked = K.liker
AND L.liker = K.liked
ORDER BY rand( )
LIMIT 0 , 1
I googled everything about joins for one week, but i cant achieve that.
I have a table with 6 columns- Date, time, action, user_id, channel, and time_and_date.
Action refers to open or close, when a user starts or end watching a tv channel.
My tasks are as following
to get an overview of the data:
- find the one-time users (who used the service only once or in only one day and
never came back) for each channel, each genre, each community
Anoother table provides the user_id, genre(news, sport....)
How can I find the one time users for those requirements?
You can try something like
SELECT FROM first_table LEFT JOIN users ON first_table.user_id=users.id GROUP BY users.id HAVING COUNT(users.id)=1
You can join your genre table after for selecting over genre channels...
To get the one-time users:
select user_id ,min(channel) as channel, min(genre) as genre, min(community) as community
from action_table
group by user_id
having min(date) = max(date);
Note the having clause. This guarantees that a users has only one date (but not necessarily one record).
This returns one value for each of the three dimensions -- for a one-time user they are the same. For someone who visits multiple times in one day, it chooses one value.
Sounds something like this:
select user_id, count(*)
from action_table
where action = 'open'
group by user_id
having count(*) = 1
order by user_id
I have two tables: tbl_users and tbl_users_jobs.
tbl_users as the use information
tbl_users_jobs as jobs, that are related to that user.
Let's say I have 3 users: id 1, 2 and 3.
User 1 has 2 jobs, user 3 has 3 jobs and user 2 has 1 job only.
I want to insert a new job to the user who has less jobs (So I can distribute jobs to all users).
How would I do that?
Need consider one thing too:
A user may start today for example and has no jobs...
Here's the table structure:
tbl_user
-------
Id
name
-
tbl_users_jobs
-------
Id
user_id
description
Thanks.
You can count jobs per user (including new users who don't have jobs) with a LEFT JOIN.
The trick is to count on a column in tbl_users_jobs because the column's value will be null for new users and will return a zero count. Just be sure to count a column that can't otherwise be null; in this case I've chosen tbl_users_jobs.Id under the assumption that it's the PK:
SELECT
tbl_user.Id,
tbl_user.Name,
COUNT(tbl_users_jobs.Id) AS JobCount
FROM tbl_user
LEFT JOIN tbl_users_jobs ON tbl_user.Id = tbl_users_jobs.user_id
GROUP BY
tbl_user.Id,
tbl_user.Name
ORDER BY JobCount DESC
Results are returned in order of who has the fewest jobs. You can take the first row as your user to get the new job, but keep in mind that there may be other users with the same number of jobs - in other words there may be 2 or 3 or more users with zero jobs. You'll have to decide how to choose between them.
I have a database table (notes) that has three columns: id, ticket, and user.
I need to show how many notes were entered by each user as well as how many unique tickets those notes were added to (grouped by user). For example, John Doe entered 100 notes into 50 unique tickets and Jane Doe entered 70 notes into 65 unique tickets. I'm not interested in which tickets they were added to, just how many.
Here is what I have so far.
select count(id) count
from notes
group by user
Is it possible to grab the number of unique tickets in the same query?
Use distinct inside the count:
select
user,
count(id) note_count,
count(distinct ticket) ticket_count
from notes
group by user
You are probably new to SQL. The query is something like:
select user, count(distinct TicketId) as NumTickets, count(id) as NumNotes
from notes
group by user
You should include the user in the select clause, so you know to whom the numbers apply. When naming things, it is a good idea to avoid reserved words like count, so I named on NumTickets and the other NumNotes.
I have two tables:
posts(id,user_id,event_id}
events(event_id,name,date]}
I want to make a query, to retrieve all the names of the events for a particular user_id say id number 2.
In pseudo-code lets say
select all the event names from posts where user_id=2
try:
select events.name from posts, events
where posts.event_id = events.event_id and user_id = 2
You will need to have something slightly different depending on how you want results with no matches to display.
select name from events join posts using (event_id) where user_id = 2