How to display Users based on their groups and attendance - mysql

I'm developing an attendance system but i'm stuck.
I have 3 tables: users, attendance_schedules and marks
users has: id, first_name, group_name, status etc columns.
attendance_schedules has: group_name, meeting_date etc columns. this table stores information about upcoming meetings
marks has: user_id, meeting_date, status etc columns. This stores a marked attendance
On the day of a meeting attendance_schedules.meeting_date, I want to display all the users that belong to the same group. However, when a user is marked, it should change the button to marked (i am using marked.status to check if it is marked)
The challenge is, when a user is marked, it displays the user twice - one for marking and the other is marked. Seems database is returning the same user (marked and unmarked)
SELECT u.first_name, m.meeting_date
FROM users AS u
LEFT JOIN marks AS m
ON u.id = m.user_id
WHERE u.status = 1
AND u.meeting_group = 'servicon'
I also tried
SELECT u.first_name, m.meeting_date
FROM users AS u
LEFT JOIN marks AS m
ON u.id = m.user_id
WHERE u.status = 1
AND u.meeting_group = 'servicon'
AND m.meeting_date = '2023-02-01'
I also tried joining the three tables but it still didnt work

Try using Select Distinct:
SELECT Distinct u.first_name, m.meeting_date
FROM users AS u
LEFT JOIN marks AS m
ON u.id = m.user_id
WHERE u.status = 1
AND u.meeting_group = 'servicon'

Related

How to count a number from different table when there's no common field?

There are a user table and a user_follow table that describes which user.id is following/followed. I'd like to count the occurrences of that user is following and being followed.
The problem is that user_follow table doesn't have user_id as a foreign key, so I'm not able to join enter image description here the two tables by a common field. I've tried to use LEFT OUTER JOIN on user.id=user_follow.following_user_id and GROUP BY user.id, but it only counts the times of following(followed times is exactly the same as the following, which is not right).
The way to solve this is to join on USER_FOLLOW twice, once for Followed By and once for Following.
You haven't posted the structure of USER_FOLLOW, so this is a guess and you'll need to correct it to fit your schema.
select u.id, u.first_name, u.last_name
, count(f.following_user_id) as following_count
, count(fb.user_id) as followed_by_count
from user u
left_outer join user_follow f on where f.user_id = u.id
left_outer join user_follow fb on where fb.following_user_id = u.id
group by u.id, u.first_name, u.last_name

Joining 3 tables, not getting desired result

I have three tables in MySql:
Events, Users both having many to many relation. Hence, third table Attend.
Table content:
Events:
e_id, e_content
Users
u_id, u_details
Attend
e_id, u_id, attending
let consider I am logged in and my uid is 1005.
So I want to see all events whether or not I am attanding the evnet but if I am attending the Attend.attending column should be yes else it should be null
I have tried a lot with joins but I have not received the desired query.
like:
select e.e_id,u.u_details,a.attending
from Events e
left join attend a on e.e_id = a.e_id
left join users u on u.u_id = a.U_id
and u.u_id = 1005;
with the above query I get same result for all uid
You need to do a LEFT JOIN here like
select e.e_id,u.u_details,a.attending
from Events e
left join attend a on e.e_id = a.e_id
left join users u on u.u_id = a.U_id
and u.u_id = 1005;
If, as is specifically asked, all events require displaying, but just where a particular user (1005) is attending has attending = "Yes" then is requires a little more than left joins, as if multiple users are attending, it will still display "Yes" for all users attending, even though the u_id is not displayed.
http://sqlfiddle.com/#!9/ed738/3/1
Shows the query with 2 left joins, and then the 2nd result set which gets what I believe is the required result.
select e.e_id,a.u_details,a.attending
from Events e
left join (select a.*, u.u_details
from attend a
INNER join users u ON
(a.u_id = u.u_id and u.u_details = 'will')) a on e.e_id = a.e_id
There are more than likely more elegant ways of doing this, but think this does the job.

MySQL SQL command to query from one table with three same columns to another table

Sorry but I am not sure how to ask this question but I am working on a help desk application where I have tickets being created in one table. I also have another table that stores the users. My problem is with the tickets table, I have listed the user that created the ticket, the tech who will solve the ticket and a user that over sees the ticket. All three users reference the users table. So how do I can I query the tickets table and get all three users that reference the same table storing the users?
Table1: Tickets
1) Ticketnumber
2) EnteredBy User 100
3) Issue
4) FixedBy User 102
5) FixedByNotes
6) ResponsilbeUser User 103
Table2: Users
1) UserID
2) UserName
What I can do now is something like this:
Select Ticketnumber, EnteredBy, Issue, UserName FROM Tickets INNER JOIN Users
ON Tickets.EnteredBy = Users.UserID
Thanks Steve
You can extend current query to somewhat as follows:
Select Ticketnumber, Issue, Reporter.UserName, Developer.UserName, Manager.UserName FROM Tickets
INNER JOIN Users AS Reporter ON Tickets.EnteredBy = Reporter.UserID
INNER JOIN Users AS Developer ON Tickets.FixedBy = Developer.UserID
INNER JOIN Users AS Manager ON Tickets.ResponsibleUser = Manager.UserID
You need alias for joint tables if you want to get all names:
Select Ticketnumber, Issue, Informers.UserName, Fixers.UserName, Supervisors.UserName FROM Tickets
INNER JOIN Users Informers ON Tickets.EnteredBy = Users.UserID
INNER JOIN Users Fixers ON Tickets.FixedBy = Users.UserID
INNER JOIN Users Supervisors ON Ticket.ResponsibleUser = Users.UserID
WHERE...
Sorry as i am not able to understand your words, but if I assumed your need correctly.. just for a try this could help you..
if you need either of them i.e. all users who has either entered or fixed or saw an issue you can find by..
Select t.Ticketnumber, t.EnteredBy, t.Issue, u.UserID ,u.UserName FROM Tickets t
INNER JOIN Users u ON t.EnteredBy = u.UserID or t.FixedBy = u.UserID
or t.ResponsibleUser = u.UserID;
And If you need all users who has entered, fixed and saw an issue you can find by..
Select t.Ticketnumber, t.EnteredBy, t.Issue, u.UserID ,u.UserName FROM Tickets t
INNER JOIN Users u ON t.EnteredBy = u.UserID and t.FixedBy = u.UserID
and t.ResponsibleUser = u.UserID;

mysql where OR having

I have a table of users, some of which have articles associated with them, and some of which have type = writer. I'd like to display all users who have articles OR who have type = writer. So, all writers should be displayed, and other user types are only displayed if they have articles.
This is my query so far, which leaves out writers with no articles.
SELECT u.name, u.type, COUNT(a.id) count
FROM users u
LEFT JOIN articles a on u.id = a.writer_id
GROUP BY u.name
HAVING count > 0
Adding the following WHERE clause obviously excludes other user types that have articles.
WHERE u.type = 'writer'
Do I need to do a UNION of these two result sets?
I think you are looking for something like this
SELECT
u.name,
u.type,
COUNT(a.id) count
FROM users u
LEFT JOIN articles a ON u.id = a.writer_id
WHERE
u.type='writer' --all users that are writers
OR
a.writer_id IS NOT NULL --all users that have at least one article
GROUP BY
u.name
--removed the having clause as it seems it may be possible that a writer has no articles.
Just change the WHERE clause to allow any user that has a matching article record:
SELECT u.name, u.type, COUNT(a.id) count
FROM users u
LEFT JOIN articles a on u.id = a.writer_id
WHERE u.type = 'writer' OR a.writer_id IS NOT NULL
GROUP BY u.name
HAVING count > 0

Multiple field report, with custom counts in MySql

I am using a system I didn't create. The system has 3 main tables:
users, courses, and usergroups. I am using an extra table called coursehits.
It's a MySQL DB, 5.0. There aren't any relationships in the DB, so users are assigned to courses by simply adding an entry to usergroups (course_id and user_id) from the courses and users table. Likewise if they start a course an entry is made to coursehits.
I am trying to count the number of users in coursehits and usergroups for reporting data. So far I have the following which doesn't count correctly in one query but this doesn't count correctly, the results are much too high:
SELECT DISTINCT
c.course_name,
COUNT(ug.user_id) AS "Enrolled",
COUNT(ch.user_id) as "Started"
FROM courses c, usergroups ug, coursehits ch, users u
WHERE ch.user_id = u.id
AND ug.user_id = u.id
AND ug.course_id = c.id
AND ch.page_id != 4
GROUP BY 1
Before I was doing the following individually which does work:
SELECT DISTINCT c.course_name, COUNT(ug.user_id) AS "Enrolled"
FROM courses c, usergroups ug
WHERE ug.course_id = c.id
GROUP BY 1
Where as now I am trying to report the status of users for each course on one query, I hope that makes sense?!?
Try:
SELECT c.course_name,
COUNT(DISTINCT ug.user_id) AS "Enrolled",
COUNT(DISTINCT ch.user_id) as "Started"
FROM courses c
left join usergroups ug on ug.course_id = c.id
left join coursehits ch on ch.course_id = c.id and ch.page_id != 4
GROUP BY c.course_name