Selecting Number of Values of a Child Table for Parent Table - mysql

On SQL, I have to return all user's name, phone and the number of tickets have, even if it's 0. I so far have tried using group by but the problem with that is it doesn't show the values of USERS that aren't associated with the ticket table and some extra random names.
User
Ticket
Here's what I've tried so far:
SELECT
U.Name,
U.Phone,
COUNT(*)
FROM
Users U
LEFT JOIN ticket T
ON U.USRID = T.USRID
GROUP BY T.USRID

I think you are looking for:
SELECT U.Name, U.Phone, COUNT(T.USRID)
FROM Users U LEFT JOIN
ticket T
ON U.USRID = T.USRID
GROUP BY U.Name, U.Phone;
Your GROUP BY columns should match the SELECT columns.

Related

Counting and Summing three tables together and get users information with their like counts

I have three tables:
table: users / columns: user_id, email, username
table: activities / columns: object, type, like_count
table: activities2 / columns: object, target_type, subject, type
The like_count in activities table has no problem and I count all of that with this query:
SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id
But there is another count in activities table which is being inserted(NOT UPDATED) each time some other types of posts liked and I counted them by this query:
SELECT activities.subject, activities.object, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject
BUT the problem starts from here when I want to join them together! I want to count like_count + count of likes that inserted in that table(activities2) that contains string of 'like_' and beside this I want to join the subject(in activities table) which is related to user_id in the other table(users).
My last query is this:
SELECT users.user_id, users.email, users.username, activities.object, COUNT(activities.type)+SUM(activities.like_count) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
INNER JOIN users
ON activities.subject = users.user_id
WHERE activities2.type LIKE 'like_%'
GROUP BY users.user_id
The problem is when joining, it fetches the user information not for the one that I want.
In conclusion I want something like this:
user_id------email-----------------username----------object-------likes
2521---------a#b.com---------------abc---------------9578---------3
5484---------c#d.com---------------def---------------8547---------16
8431---------e#f.com---------------ghi---------------4584---------1
And offcourse the result is this but only likes are correct however columns of user_id, email, username that are in users table NOT!
I was wondering if you would help to fix it. I'm really tired of trying and facing to no result :(
users table data:
user_id-------------email---------------username
1-------------------a#b.com-------------abc
2-------------------c#d.com-------------def
3-------------------e#f.com-------------ghi
activities table data:
object----------type----------------like_count------subject
20--------------like_video----------0---------------1
20--------------like_photo----------0---------------2
33--------------like_music----------0---------------3
33--------------some_other_type-----5---------------6
33--------------some_other_type-----8---------------10
activities2 table data:
object------target_type-----subject-----type
20----------parent----------30----------like_video
21----------owner-----------40----------like_audio
22----------parent----------50----------something_not_start_with_like_
I want:
user_id------email-----------------username----------object-------likes
1------------a#b.com---------------abc---------------9578---------(sum of like_count + count of type which has like_ in first characters)
2------------c#d.com---------------def---------------8547---------(sum of like_count + count of type which has like_ in first characters)
3------------e#f.com---------------ghi---------------4584---------(sum of like_count + count of type which has like_ in first characters)
Consider joining the aggregate queries using derived tables and then run your addition calculation in the outer query. Also, below object column is removed from the second aggregate query's GROUP BY clause but still used in JOIN since you need summation at the subject level.
SELECT u.user_id, u.email, u.username, a.`object`, u.likes + a.likes as `total_likes`
FROM
(SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id, users.email, users.username) u
INNER JOIN
(SELECT activities.subject, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject) As a
ON u.user_id = a.subject

Result of one query, input for other query in mysql

I want to get information from two tables users and results.
users table has
id, name, email, password columns etc.
and results table has
id, user_id, last_attempt_time etc.
The result table gets populated every time a user takes a quiz.
I want to display user_id,name and last_attempt_time but my query returns oldest time and i have no idea how to solve this problem.
SELECT u.id,u.email,u.name,u.joined,r.last_attempt_time FROM users u
LEFT JOIN results r
ON u.id=r.user_id
GROUP BY u.id
ORDER BY u.id ASC
Try:
SELECT u.id,u.email,u.name,u.joined,MAX(r.last_attempt_time) AS LastAttempt
FROM users u left join results r on u.id=r.quiz_id
GROUP BY u.id,u.email,u.name,u.joined
ORDER BY u.id ASC

Join 3 tables to get single row

I'm trying to join multiple table to get single row result for each id. This result will be send to angular UI
I have 3 tables user, friends and trip
A user can have multiple friends but one trip
I like to get all details corresponding to a user in one row, probably friends as field like an array?
This is how my table looks.
http://sqlfiddle.com/#!9/0879d/2
https://gist.github.com/tomalex0/9dee4fff85583732e7d0
group_concat should do the trick for you:
SELECT u.*, t.*, friendlist
FROM user u
LEFT JOIN trip t ON u.id = t.user_id
LEFT JOIN (SELECT user_id, GROUP_CONCAT(CONCAT (name, '- ', email)) friendlist
FROM friends
GROUP BY user_id) f ON f.user_id = u.id

Combining RIGHT JOIN with COUNT

I'm trying to get a list of the number of entries in the changes_cc table by each user. Not all users have made entries into it, however for some reason it's returning "1" for each user that has 0 entries. I'm assuming that it's because it's counting the entries in the JOINed table. How can I make it so that it is "0" instead?
SELECT COUNT(*) as num, users.id, realname, username
FROM changes_cc
RIGHT JOIN users
ON changes_cc.user_id = users.id
GROUP BY users.id
I think this should work -- count a specific field in the changes_cc table vs counting *:
SELECT u.id, realname, username, COUNT(c.id) as num
FROM users u
LEFT JOIN changes_cc c
ON u.user_id = c.id
GROUP BY u.id
I prefer reading a LEFT JOIN over a RIGHT JOIN, but they are both OUTER JOINs and work the same.
You should not be using COUNT(*) (counts the record including null values) because it will normally give atleast 1 since it returns all records from the right table. If you specify the column name to be counted, it will gove you the result you want because COUNT only counts for NON_NULL value.
SELECT COUNT(changes_cc.user_id) as num,
users.id,
realname,
username
FROM changes_cc
RIGHT JOIN users
ON changes_cc.user_id = users.id
GROUP BY users.id
Instead of using count(*), use count(changes_cc.user_id).
The problem is that you are counting rows (with the *) rather than counting the non-NULL values in the "right-joined" table.

Table join issue with MySQL

I have a table for referred users (contains an email address and date columns) and a table for users.
I run to get the top referers:
SELECT count(r.Email) as count, r.Email
FROM refs r
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
But I want to join this with the users table so it displays with other data in the user table, I thought a join would work. Left join becuase emails may be entered incorrectly, some people put first name etc under refs.Email
SELECT count(r.Email) as count, r.Email, u.*
FROM refs r LEFT JOIN users u ON u.email_primary = r.Email
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
With the above query the count is incorrect, but I don't know why.
Try this one:
SELECT count(r.Email) as count, r.Email
FROM refs r
INNER JOIN users u ON u.email_primary = r.Email
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
if your adding new column from users u you also need to add it on your group by clause.
Regards
Unfortunately, a LEFT JOIN wont help you here; what this type of join says is give me all the rows in users that match my email, as well as all the rows that have no match on email. If the email doesn't match, then they wont come back as you want.
So you can't use a the left join condition here the way you want.
If you enforced the fact that they had to enter an email everytime, and it was a valid email etc, then you could use an INNER JOIN.
JOINs are usually used to follow referential integrity. So, for example, I have a user with an id in one table, and another table with the column userid - there is a strong relationship between the two tables I can join on.
Jeft Atwood has a good explantion of how joins work.
SEE if this will help you:
SELECT e.count, e.email, u.col1, u.col2 -- etc
FROM (
SELECT count(r.Email) as count, r.Email
FROM refs r
WHERE r.referredOn > '2011-12-13'
GROUP BY email
) e
INNER JOIN
users u ON u.email_primary = e.Email
Instead of a direct join, you could TRY to use your counting query as a subquery-table type..
I wrote this query
SELECT *, count(r.Email) as count FROM refs r
LEFT OUTER JOIN users u ON r.email = u.email_primary
WHERE u.uid IS NOT NULL
GROUP BY u.uid
ORDER BY count DESC
Which showed me that the reason the count was wrong was because some of the email addresses are used twice in the users table (users sharing 'family' email address), this doubled my count, the above query shows each separate user account.