Query also returns column with value 0 - mysql

I have two tables:
tb_question
**id_quest** | **desc_quest**
1 | How do you do...?
2 | How are you...?
tb_answer
**id_quest** | **date_answer**
1 | 2013/11/25
1 | 2013/11/26
1 | 2013/11/27
And my query:
SELECT
q.id_quest,
q.desc_quest,
COUNT(a.id_quest) as total_answer -- count total answer by question
FROM tb_question q
INNER JOIN tb_answer a
ON q.id_quest = a.id_quest;
Result:
ID_QUEST | DESC_QUEST | TOTAL_ANSWER
1 | How do you do...? | 3
How could it do to return also question id = 2 with 0 count ?
Expected result:
ID_QUEST | DESC_QUEST | TOTAL_ANSWER
1 | How do you do...? | 3
2 | How are you...? | 0
http://sqlfiddle.com/#!2/3bfe7/1

Two problems:
You have to use an outer join to get questions with no answers.
You left out the GROUP BY clause.
SELECT
q.id_quest,
q.desc_quest,
COUNT(a.id_quest) as total_answer
FROM tb_question q
LEFT OUTER JOIN tb_answer a ON q.id_quest = a.id_quest
GROUP BY q.id_quest
DEMO

dont use inner join to select all idquest, inner join to produce subset of table
SELECT tbq.id_quest,tbq.desc_quest,COUNT(tba.id_quest)
FROM tb_question tbq
LEFT OUTER JOIN tb_answer tba ON tba.id_uest=tbq.id_quest
GROUP BY tbq.id_quest
ORDER BY tbq.id_quest

Related

How to count in mysql

I have this query in mySQL where I would like to sum the line product of each doctor but I dont know how to do it.
use avant_medical;
select
sales.doctor_id as DoctorID,
line_products.id as LineProductID,
line_products.name as LineProductName
from `doctors`
inner join `sales` on `doctors`.`id` = `sales`.`doctor_id`
inner join `inventories` on `sales`.`id` = `inventories`.`sale_id`
inner join `products` on `inventories`.`product_id` = `products`.`id`
inner join `line_products` on `products`.`lineProduct_id` = `line_products`.`id`
order by `doctors`.`id` asc;
lPID= lineProductID
|DrID|lPID |
| -- | ----|
| 1 | 7 |
| 1 | 6 |
| 1 | 6 |
| 1 | 7 |
| 1 | 7 |
| 1 | 7 |
| 1 | 6 |
This is how I want:
Doctor 1
lineID | quantity
7 | 4
6 | 3
I try this query only in mySQL
The keyword you are looking for is count, not sum. Summing would add up every lineProductID as if they where regular mathematical values, while counting will add up how many times a given lineProductID is found.
select
sales.doctor_id as DoctorID,
line_products.id as LineProductID,
line_products.name as LineProductName,
-- We count the number of occurrences of each line_product.id
COUNT(line_products.id) as LineProductQty
from `doctors`
inner join `sales` on `doctors`.`id` = `sales`.`doctor_id`
inner join `inventories` on `sales`.`id` = `inventories`.`sale_id`
inner join `products` on `inventories`.`product_id` = `products`.`id`
inner join `line_products` on `products`.`lineProduct_id` = `line_products`.`id`
-- Never forget to properly GROUP your aggregate functions, such as COUNT() or SUM()!
GROUP BY sales.doctor_id, line_products.id, line_products.name
order by `doctors`.`id` asc;
Since you didn't provided full schema to test this, I made a small, very artificial demo, but should be representative of how the query above works.

MySQL Summing & Counting Child Values in Another Table

BOOKINGS TABLE
id | price | anotherVal
-----------------------------
1 | 10000 | *
2 | 20000 | *
3 | 1000 | *
4 | 8000 | *
BOOKING PAYMENTS TABLE
id | bookingId | amount | currencyId | mxnAmount
--------------------------------------------------
1 | 1 | 100.00 | 1 | 100.00
2 | 1 | 300.00 | 3 | 6400.00
3 | 2 | 500.21 | 1 | 500.21
4 | 4 | 123.95 | 6 |
4 | 4 | 800.00 | 1 | 800.00
I need to get all BOOKINGS_TABLE columns and then for each booking add up the mxnAmount column, but also the result should tell if all rows in BOOKING_PAYMENTS_TABLE had an mxnAmount so i can know if the mxnAmount is final or there's some rows left to be updated, i have a query that works for the first part:
SELECT b.*, SUM(p.mxnAmount) FROM bookings b LEFT JOIN bookingPayments p ON b.id = p.bookingId GROUP BY b.id
I figured i could make us of COUNT() to count all rows in BOOKING_PAYMENTS_TABLE but then how can i get the number for the rows that have an mxnAMOUNT?
SELECT b.*, SUM(p.mxnAmount), COUNT(p.id) FROM bookings b LEFT JOIN bookingPayments p ON b.id = p.bookingId GROUP BY b.id
I tried this:
SELECT b.*, SUM(p.mxnAmount), COUNT(p.id), COUNT(pp.id) FROM bookings b LEFT JOIN bookingPayments p ON b.id = p.bookingId LEFT JOIN bookingPayments pp ON b.id = pp.bookingId WHERE pp.mxnAmount IS NOT NULL GROUP BY b.id
But then the query returns only bookings that have all their payments rows with an mxnAmount, any leads?
I figured i could make us of COUNT() to count all rows in BOOKING_PAYMENTS_TABLE but then how can i get the number for the rows that have an mxnAMOUNT?
Just COUNT() that particular column: this gives you the number of non-null values in the column for each group:
SELECT b.*, SUM(p.mxnAmount), COUNT(p.id), COUNT(p.mxnAmount)
FROM bookings b
LEFT JOIN bookingPayments p ON b.id = p.bookingId
GROUP BY b.id
If you want to know if any mxmamount in the group is missing, you can do:
MAX(p.id IS NOT NULL AND p.mxnAmount IS NULL) has_missing_mxnAmount

How to join 5 tables MYSQL with specific condition

i have 5 tables below
tb_satker
kdsatker
1
2
3
tb_akun
akun | code
A | 1
B | 2
C | 3
tb_simponi
kdsatker | akun | jumlah
1 | A | 100
1 | B | 200
tb_span
kdsatker | akun | jumlah
1 | A | 1
1 | B | 2
1 | C | 3
tb_upt
kdsatker | akun | jumlah
1 | A | 10
1 | B | 20
1 | C | 30
What i need is output to something like this
kdsatker | akun | simponi | span | upt
1 | A | 100 | 1 | 10
1 | B | 200 | 2 | 20
1 | C | 0 | 3 | 30
2 | 0 | 0 | 0 | 0
3 | 0 | 0 | 0 | 0
I've try using sql fiddle but the result is not right (http://sqlfiddle.com/#!9/e83ce7/10)
SELECT tb_satker.kdsatker,tb_akun.akun,tb_simponi2.simponi,tb_span2.span,tb_upt2.upt
FROM tb_satker
LEFT JOIN (SELECT kdsatker,akun,sum(tb_simponi.jumlah) as simponi FROM tb_simponi GROUP BY kdsatker,akun)
as tb_simponi2 ON tb_simponi2.kdsatker=tb_satker.kdsatker
LEFT JOIN (SELECT kdsatker,akun,sum(tb_span.jumlah) as span FROM tb_span GROUP BY kdsatker,akun)
as tb_span2 ON tb_span2.kdsatker=tb_satker.kdsatker
LEFT JOIN (SELECT kdsatker,akun,sum(tb_upt.jumlah) as upt FROM tb_upt GROUP BY kdsatker,akun)
as tb_upt2 ON tb_upt2.kdsatker=tb_satker.kdsatker
LEFT JOIN
tb_akun ON tb_akun.akun=tb_simponi2.akun AND
tb_akun.akun=tb_span2.akun AND
tb_akun.akun=tb_upt2.akun
GROUP BY tb_satker.kdsatker,tb_akun.akun
Can anybody help me with the right idea??
Many Thanks
Your inner queries are all based on a two-part group by two parts... kdsatker and akun.
Since it is possible for any of the tb_simponi, tb_span, tb_upt can have any combination,
I would pre-union all possible combinations of those FIRST. THEN, Join to the summary results.
and FINALLY get the akun and kdsatker components. Something like...
SELECT
tb_satker.kdsatker,
coalesce( tb_akun.akun, 0 ) akun,
coalesce( tb_simponi2.simponi, 0 ) simponi,
coalesce( tb_span2.span, 0 ) span,
coalesce( tb_upt2.upt, 0 ) upt
FROM
tb_satker
LEFT JOIN
( SELECT distinct kdsatker, akun FROM tb_simponi
UNION
SELECT kdsatker, akun FROM tb_span
UNION
SELECT kdsatker, akun FROM tb_upt
) AllKdAkun
on tb_satker.kdsatker = AllKdAkun.kdsatker
LEFT JOIN tb_akun
on AllKdAkun.akun = tb_akun.akun
LEFT JOIN
(SELECT kdsatker, akun, sum(tb_simponi.jumlah) as simponi
FROM tb_simponi
GROUP BY kdsatker, akun ) as tb_simponi2
ON AllKdAkun.kdsatker = tb_simponi2.kdsatker
AND AllKdAkun.Akun = tb_simponi2.akun
LEFT JOIN
(SELECT kdsatker, akun, sum(tb_span.jumlah) as span
FROM tb_span
GROUP BY kdsatker, akun ) as tb_span2
ON AllKdAkun.kdsatker = tb_span2.kdsatker
AND AllKdAkun.akun = tb_span2.akun
LEFT JOIN
(SELECT kdsatker, akun, sum(tb_upt.jumlah) as upt
FROM tb_upt
GROUP BY kdsatker, akun) as tb_upt2
ON AllKdAkun.kdsatker = tb_upt2.kdsatker
AND AllKdAkun.akun = tb_upt2.akun
No final group by at the outer level.
First level is your tb_satker. There may (or not) be a record in any of the subsidiary tables, so that is first.
Second level is a distinct list of every kdsatker, akun in ANY of the other 3 tables. So now you can LEFT JOIN kdsatker thus keeping every record including 1, 2, 3 even though 2 & 3 have no records.
From the second level, you can now left-join to the tb_akun table which in this case, only kdsatker is the only one with records for any given akun.
Finally joining the pre-union list of all combinations grouped by kdsatker, akun are able to be matched for their final summary into the final list.
Hope each step makes sense to what you appear to be trying for.

Mysql select rows with same id's (3 tables)

I have the following tables:
'blog_content'
'blog_media'
'blog_media_content'
| blog_id | media_id |
========================
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 3 | 4 |
I want to select all blog_media.uri's where blog_media.media_id equals blog_media_content.blog_id.
Please help me to achieve my aim.
An inner join between blog_media and blog_media_content tables would suffice.
SELECT
bm.uri
FROM blog_media bm
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
WHERE bmc.blog_id =3;
Note:
If you need any additional information from blog table then you need an additional inner join like below:
...INNER JOIN blog_table b ON bmc.blog_id = b.blog_id...
EDIT:
In order to get records for all blog_ids :
SELECT
bm.uri
FROM blog_media bm
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
ORDER BY bmc.blog_id;

query which creates missing rows based on anther table

I have many forms that users fill out. Each form contains a list of questions. In this first table is the form id and the id's of the questions.
form_id | question_id
1 | 1
1 | 2
1 | 3
2 | 4
2 | 5
This table has two forms one which has 3 questions and the other 2. I have a second table which has the answers that the users have given for the questions.
user_id | form_id | question_id | answer
476 | 1 | 1 | "answer1"
476 | 1 | 3 | "answer2"
693 | 1 | 1 | "answer3"
693 | 1 | 2 | "answer4"
235 | 2 | 5 | "answer5"
In this example, 2 users have filled out form 1 and 1 user has filled in form 2. But none have filled in all the questions. Is it possible to write a query which combines the two tables and will give me the answers that the user have given including the questions that they didn't answer? I'd like the results to look like this.
user_id | form_id | question_id | answer
476 | 1 | 1 | "answer1"
476 | 1 | 2 | NULL
476 | 1 | 3 | "answer2"
693 | 1 | 1 | "answer3"
693 | 1 | 2 | "answer4"
693 | 1 | 3 | NULL
235 | 2 | 4 | NULL
235 | 2 | 5 | "answer5"
The problem that I have when I use a left join like this
select * from template t
left join answers a on a.template_id = t.template_id
AND a.question_id = t.question_id
AND t.template_id = t.template_id;
is that the row that results is missing user_id.
Yes, the specified result can be returned by a query.
One way to achieve this is a join to an inline view, and an "outer join" operation to the second table.
The "trick" is getting a distinct list of user_id and form_id from the second table, using a query, for example:
SELECT user_id, form_id
FROM second_table
GROUP BY user_id, form_id
And then using that query as an inline view (wrapping it in parens, assigning a table alias, and referencing it like it was a table in an outer query.
All that's required after that is an "outer join" to the second table.
For example:
SELECT r.user_id
, q.form_id
, q.question_id
, a.answer
FROM first_table q
JOIN ( SELECT p.user_id, p.form_id
FROM second_table p
GROUP BY p.user_id, p.form_id
) r
ON r.form_id = q.form_id
LEFT
JOIN second_table a
ON a.user_id = r.user_id
AND a.form_id = r.form_id
AND a.question_id = q.question_id
ORDER
BY r.user_id
, q.form_id
, q.question_id
Note that the keyword "LEFT" specifies an outer join operation, returning all rows from the left side, along with matching rows from the right side. A typical "inner" join would exclude rows that didn't find a matching row from the table on the right side.
use
left join
something like:
select * from table1 left join table2 on table1.form_id= table2.form_id