mysql subqueries total between 2 tables - mysql

I tried some subqueries but I didn't succed.
Let's suppose we have 2 tables : users(iduser, name), invoices(idinvoice, title, status, iduser)
and we want to show for each user, total invoices "paid", total invoices "unpaid" (that's status field)
I tried query like that
Select users.*, total_invoices_paid, total_invoices_unpaid
from users
LEFT JOIN (SELECT iduser, sum(if(status='paid',1,0)) AS total_invoices_paid, sum(if(status='unpaid',0,1)) AS total_invoices_unpaid
FROM invoices GROUP BY invoices.idinvoice) AS subinvoices
ON subinvoices.iduser=users.iduser
But I got wrong values, I think I missed something, but don't know what
any help please?
Thanks

I think you want this:
select u.*,
sum(i.status = 'paid') as total_invoices_paid,
sum(i.status = 'unpaid') as total_invoices_unpaid
from users u
left join invoices i on u.iduser = i.iduser
group by u.iduser;
A boolean is evaluated to 1 or 0 for true or false respectively in MySQL. Hence, using sum on the condition, we can find count.
Using subquery:
select u.*,
coalesce(i.total_invoices_paid, 0) as total_invoices_paid,
coalesce(i.total_invoices_unpaid, 0) as total_invoices_unpaid,
coalesce(i.total_invoices, 0) as total_invoices,
coalesce(i.total_paid_and_unpaid, 0) as total_paid_and_unpaid,
coalesce(i.total_with_non_null_status, 0) as total_with_non_null_status
from users u
left join (
select iduser,
sum(status = 'paid') as total_invoices_paid,
sum(status = 'unpaid') as total_invoices_unpaid,
count(1) as total_invoices,
sum(status in ('paid','unpaid')) as total_paid_and_unpaid,
count(status) as total_with_non_null_status
from invoices
group by iduser
) i on u.iduser = i.iduser
group by u.iduser;

Related

How to select sql data by using 2 join tables with OR condition without duplicate records

I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);

Selecting from three tables

I am trying to SELECT from one table and count from two other tables based on the rows from the first table. I tried the following code below but the rows keep coming empty.
SELECT list.id, list.title, list.body, list.poster, list.created_at, count(comments.id) as comcount, count(supports.topic_id) as supcount
FROM (
SELECT *
FROM topics
ORDER BY created_at DESC
LIMIT 5
) AS list, comments, supports
WHERE
list.id = comments.id OR
list.id = supports.topic_id
Through in this scenario table topics has only two rows and tables comments and supports have no rows in them, but yet still I should be able to get two rows with their aliases supcount and comcount each having a value 0 as an output.
I got the solution to the above but am trying something else with the solution provided which I explained in the comment area of the solution provided.
SELECT
t.id,
t.title,
t.body,
t.poster,
t.created_at,
s.supporter,
IFNULL((SELECT COUNT(*) FROM comments c WHERE c.id = t.id), 0) AS comcount,
IFNULL((SELECT COUNT(*) FROM supports s WHERE s.topic_id = t.id), 0) AS supcount,
CASE WHEN (s.supporter = "Davies Alex") THEN '1' ELSE '0' END sup,
CASE WHEN (c.commenter = "Davies Alex") THEN '1' ELSE '0' END com
FROM topics t, comments c, supports s
ORDER BY created_at DESC
This gonna be working, give a try (using subquery for just counting entries in another table is more suitable):
SELECT
id,
title,
body,
poster,
created_at,
IFNULL((SELECT COUNT(*) FROM comments c WHERE c.id = t.id), 0) AS comcount,
IFNULL((SELECT COUNT(*) FROM supports s WHERE s.topic_id = t.id), 0) AS supcount
FROM topics t
ORDER BY created_at DESC
LIMIT 5
Update for new requirement:
SELECT
t.id,
t.title,
t.body,
t.poster,
t.created_at,
s.supporter,
IFNULL(COUNT(c.id), 0) AS comcount,
IFNULL(COUNT(s.id), 0) AS supcount,
SUM(IF(s.supporter IS NOT NULL AND s.supporter = "Davies Alex", 1, 0)) > 0 AS sup,
SUM(IF(c.commenter IS NOT NULL AND c.commenter = "Davies Alex", 1, 0)) > 0 AS com
FROM topics t
LEFT JOIN comments c ON c.id = t.id
LEFT JOIN supports s ON s.topic_id = t.id
GROUP BY t.id
ORDER BY created_at DESC
In your query, you require list.id to either match comments.id or supports.topic_id. If you use an outer join, you'll be able to retrieve data from the initial table even though the joined tables don't match or contain any data.
SELECT
topics.id, topics.title, topics.body, topics.poster, list.created_at,
count(comments.id) as comcount,
count(supports.topic_id) as supcount
FROM lists
LEFT JOIN comments ON comments.id = topics.id
LEFT JOIN supports ON supports.topic_id = topics.id
ORDER BY created_at DESC
LIMIT 5

MySQL counting elements without using count(*)

I'm practicing MySQL for an upcoming exam and need some help.
I have this db:
USER(Code, Name, Surname, Age)
THEATRE(Name, City, Capacity)
SUBSCRIPTION(ID, UserCode, TheatreName, Amount)
With these referential integrity constraints:
SUBSCRIPTION.UserCode->USER.Code
SUBSCRIPTION.TheatreName->THEATRE.Name
For exercise I need to write the query which determines code, name and surname of the users older than 50 and who has more than one subscription WITHOUT using the COUNT function.
I know that maybe a self-join could help but I really don't know how. Can anyone help me? Thank you very much.
You can use
EXISTS:
SELECT u.Code, u.Name, u.Surname
FROM USER u
WHERE u.Age > 50
AND EXISTS (
SELECT 1 FROM SUBSCRIPTION s WHERE u.Code = s.UserCode
)
Or JOIN
SELECT DISTINCT u.Code, u.Name, u.Surname
FROM USER u
JOIN SUBSCRIPTION s
ON u.Code = s.UserCode
WHERE u.Age > 50
Edited:
SELECT DISTINCT u.Code, u.Name, u.Surname
FROM USER u
JOIN SUBSCRIPTION s1
ON u.Code = s1.UserCode
JOIN SUBSCRIPTION s2
ON u.Code = s2.UserCode
WHERE s1.ID <> s2.ID
AND u.Age > 50
I believe the simplest way to accomplish this is to essentially redesign the count function into a sum function with a case statement thusly:
SELECT
u.NAME
, u.SURNAME
, u.CODE
, SUM(CASE WHEN t.SUBSCRIPTION IS NOT NULL THEN 1 ELSE 0 END) as TOTAL_SUBSCRIPTIONS -- IDENTICAL TO COUNT(s.*)
, COUNT(s.*) -- SHOULD MATCH THE TOTAL_SUBSCRIPTIONS
FROM
USER AS u
LEFT JOIN SUBSCRIPTION AS s
ON u.CODE = s.USERCODE
-- LEFT JOIN THEATRE AS t -- commented because I don't see a requirement for this table to be brought forward.
-- ON s.THEATRENAME = t.NAME
WHERE u.AGE > 50
HAVING SUM(CASE WHEN t.SUBSCRIPTION IS NOT NULL THEN 1 ELSE 0 END) > 1
Without using a CASE statment:
SELECT
u.NAME
, u.SURNAME
, u.CODE
, SUM( (select SUM(1) from SUBSCRIPTION WHERE s.USERCODE = u.CODE) ) as TOTAL_SUBSCRIPTIONS -- IDENTICAL TO COUNT(s.*)
FROM
USER AS u
WHERE u.AGE > 50

Count, SUM, LEFT JOIN and GROUP BY in query not working right

I have tried a few things but I can't seem to figure out what's causing the problem.
When I remove the totalHours part, the query works fine. But with it, it displays the right number of hours but the wrong number of Jobs, Selected and Winners.
Could someone please tell me what I am doing wrong?
Thanks in advance.
Here is my query;
SELECT
crmCandidate.candidateID,
crmCandidate.candidateName,
COUNT(DISTINCT crmJoin.joinID) AS Jobs,
SUM(IF(crmJoin.joinExtra = 'select', 1, 0)) AS Selected,
SUM(IF(crmJoin.joinExtra = 'winner', 1, 0)) AS Winner,
ROUND(SUM(crmDays.total)) AS totalDays
FROM crmCandidate
LEFT JOIN crmJoin ON (crmJoin.joinChild = crmCandidate.candidateID)
LEFT JOIN crmJob ON (crmJob.jobID = crmJoin.joinParent)
LEFT JOIN crmDays ON (crmDays.dayCandidateID = crmJoin.joinChild)
WHERE
crmDays.dayJobID = crmJob.jobID AND
crmDays.dayCandidateID = crmCandidate.candidateID
GROUP BY
crmCandidate.candidateID
ORDER BY DESC
LIMIT 100
try this one :
SELECT
crmCandidate.candidateID,
crmCandidate.candidateName,
COUNT(DISTINCT crmJoin.joinID) AS Jobs,
Sum(Case When crmJoin.joinExtra = 'select' Then 1 else 0 end) as Selected,
Sum(Case When crmJoin.joinExtra = 'winner' Then 1 else 0 end) as winner,
ROUND(SUM(crmDays.total)) AS totalDays
FROM crmCandidate
LEFT JOIN crmJoin
ON crmJoin.joinChild = crmCandidate.candidateID
LEFT JOIN crmJob
ON crmJob.jobID = crmJoin.joinParent
Inner JOIN crmDays
On crmDays.dayCandidateID = crmCandidate.candidateID
AND crmDays.dayJobID = crmJob.jobID
GROUP BY crmCandidate.candidateID, crmCandidate.candidateName
ORDER BY candidateID DESC
LIMIT 100
The best thing to do is to aggregate the data before you do the join. You can probably do what you want with the following count(distinct) clauses:
COUNT(DISTINCT case when crmJoin.joinExtra = 'select' then crmJoin.JoinId end) AS Selected,
COUNT(DISTINCT case when crmJoin.joinExtra = 'winner' then crmJoin.JoinId end) AS Winner,

Some sort of join statement?

How do I put these two queries into a single query?
select count(id) as cnt from {$site_id}.proofingv2_packages where active='1'
select count(id) as cnt from {$site_id}.proofingv2_package_options where active='1' and parent={$row["id"]} order by sort
$row['id'] is the id field from the first query. I am trying to determine if there are any valid packages. A valid package must be active and have at least 1 active option. Running 2 queries for this doesn't seem right.
Can anyone help?
select count(id) as cnt from
{$site_id}.proofingv2_packages pp
INNER JOIN
{$site_id}.proofingv2_package_options
pt ON pp.active = pt.Active AND
pp.Active = 1
if the id is the PK or FK on the same on both tables use this query
select count(id) as cnt from
{$site_id}.proofingv2_packages pp
INNER JOIN {$site_id}.proofingv2_package_options pt ON pp.id= pt.id
AND pp.Active = 1
SELECT IF(count(*) > 0, 1, 0) AS isValid
FROM {$site_id}.proofingv2_packages pp
INNER JOIN {$site_id}.proofingv2_package_options ppo ON ppo.parent = pp.id
WHERE pp.active = '1'
AND ppo.active = '1'
This should return 1 if there are valid packages or 0 if not