How to use having count less than another table - mysql

I am having trouble with my MySQL query. I want to show the id FROM one table using left join 3 another table.
What do I want is :
Show id IS NULL in 1 of another table
And having COUNT() < (get column in another table)
I tried this but it is still wrong:
SELECT p.id FROM penerimaan AS p
LEFT JOIN perangkat AS per ON per.id_penerimaan=p.id
LEFT JOIN permintaan AS pa ON pa.id=p.id_permintaan
LEFT JOIN konfirmasi_permintaan AS k ON k.id_permintaan=pa.id
WHERE per.id_penerimaan IS NULL
GROUP BY p.id
HAVING COUNT(per.id_penerimaan) < k.jumlahConfirm //how to get column in another table
ORDER BY p.id ASC
the table I have
table permintaan
id jumlah status
2 3 Confirmed
3 5 Confirmed
-----------------------------------------------
table penerimaan
id id_permintaan date
1 2 2017-07-12
2 3 2017-08-12
-----------------------------------------------
table konfirmasi_permintaan
id id_permintaan jumlahConfirmed
1 2 3
2 3 3
-----------------------------------------------
table perangkat
id id_penerimaan serial type
1 1 766544 SG90D-08-AS
2 1 552411 SLM2008T-EU
3 1 552411 SLM2008T-TU
4 2 561434 SG95-24-AS
I desired result like this
id_penerimaan
2
though id_penerimaan in table perangkat IS NULL but still show, because count(perangkat.id_penerimaan) is 2 in table perangkat less than jumlahConfirm in table konfirmasi_permintaan
Thank you

Removing WHERE per.id_penerimaan seems to solve the problem. You also have to add k.jumlahConfirmed to the SELECT list, because HAVING can only access values in the select list, not columns from the original tables.
SELECT p.id, k.jumlahConfirmed FROM penerimaan AS p
LEFT JOIN perangkat AS per ON per.id_penerimaan=p.id
LEFT JOIN permintaan AS pa ON pa.id=p.id_permintaan
LEFT JOIN konfirmasi_permintaan AS k ON k.id_permintaan=pa.id
GROUP BY p.id
HAVING COUNT(per.id_penerimaan) < k.jumlahConfirmed
DEMO

Related

Can I force row headers to show on an Access crosstab with dynamic column headers?

I have tblOuts that tracks Skus out of inventory by Category and date:
OutDate
Category
Sku
20210322
A
111
20210322
B
222
20210323
A
111
20210323
B
222
20210323
B
333
20210324
D
444
I created a crosstab that will show the count of Skus by Category and OutDate:
Category
20210322
20210323
20210324
A
1
1
B
1
2
D
1
How can I modify the crosstab to also show Category C, even though it doesn't have any data in my table yet?
Category
20210322
20210323
20210324
A
1
1
B
1
2
C
D
1
Normally I would create a separate table that listed all Categories and left join it to the crosstab. But I can't think of an ideal way to do so.
SELECT tblCategories.Category, qryCrosstab.*
FROM tblCategories LEFT JOIN qryCrosstab ON tblCategories.Category = qryCrosstab.Category
gives me two Category fields, which I don't want:
tblCategories.Category
qryCrosstab.Category
20210322
20210323
20210324
Alternately, I could do the following, but would need to modify my query every time a new date is added to the table:
SELECT tblCategories.Category, qryCrosstab.20210322, qryCrosstab.20210323, qryCrosstab.20210324
FROM tblCategories LEFT JOIN qryCrosstab ON tblCategories.Category = qryCrosstab.Category
Yes, I see what you mean by "doubled":
SELECT
Category.Category,
qOut.*
FROM
Category
LEFT JOIN
qOut ON Category.Category = qOut.Category;
Can't you just ignore one of those category fields?

Count rows in table with certain value in other table

I need a quick way to find the Number of items in a table. The items are linked to an other table. Table 1 is products and table 2 is orders.
Orders contains a paid status (1 or 0).
Orders table example:
id paid
1 0
2 1
Products table example:
id orderid type
1 1 5
2 1 5
3 1 3
4 2 5
5 2 5
6 2 3
Products contains a id (orderid) that refers to the order and a type. So i need the number of products where type = 5 and paid = 1 in the orders table.
What is the best and fastest way to archieve this?
So I need all the paid products with type 5. The result should be '2'.
you can use join like this,
SELECT COUNT(*) AS num_rows
FROM products
LEFT JOIN orders ON orders.id = products.orderid
WHERE type = 5 AND paid = 1
One way is to use a join statement. Making some assumptions about your schema, the following should work:
SELECT COUNT(p.`id`) FROM `products_table` p
LEFT JOIN `orders_table` o ON p.`orderid` = o.`id`
WHERE o.`paid` = 1
AND p.`type` = 5

Need to display records join results as well as remaing qty without join

i have 3 tables. need to display records join results as well as remaing qty without join
tblBookhead
BookHeadId, bookname, bookauthor,...
tblBookDetail
BookHeaddetailId,BookHeadId,Qty
tblBookorder
orderid,BookHeaddetailId , orderqty
select * from tblBookhead bh inner join tblBookDetail bd on
bh.BookHeadId=bd.BookHeadId inner join tblBookorder br on
br.BookHeaddetailId=db.BookHeaddetailId where BookHeadId=1;
this will retrun good results no problem.
but now if qty is 10 for BookHeadId=1 and orderqty=5 then result need to display remaing 5 qty without join (my expectation is null values for remaining qty in tblBookorder )
my expected result would be:
BookHeadId, bookname, bookauthor,... BookHeaddetailId,BookHeadId,Qty,orderid,BookHeaddetailId , orderqty
1 1 john 1 1 10 1 1 5
1 1 john 1 1 10 null null null

Mysql find matching rows for common field in two tables and combine fields into one row

I have three tables:
Table 'A' has a number of events each with a unique id.
Table 'B' has categories which match the id of table A though there could be several matches for each id.
Table 'C' defines category names which match an id in table 'B'.
How can I get an output that shows unique events that have multiple categories listed together as opposed to each event repeated per category?
Table A Table B Table C
id | event | date | id | catid catid | cat
1 swim 1-2-16 1 11 11 slow
2 swim 1-2-16 1 12 12 med
3 run 1-3-16 1 13 13 fast
4 bike 1-5-16 2 11
5 run 1-30-16 3 12
3 13
4 12
5 11
I have this Mysql statement:
SELECT A.*, B.*, C.*
FROM A, B, C
WHERE A.id = B.id and B.catid = C.catid and DATE(date) BETWEEN "1-2-16" and "1-5-16
ORDER BY event, cat
Problem is the output of this statement repeats the event for each category. I would like the output to list all categories applicable for each event as an output row. For example, "swim on 1-2-16 slow,med,fast" instead of "swim on 1-2-16 slow" "swim on 1-2-16 med" "swim on 1-2-16 fast". How can I accomplish this?
You should use group_concat
select A.event, A,date, group_concat(C.cat)
from A
inner join b on a.id = b.id
inner join b.catid = c.catid
group by b.id;

mysql left join produces fewer results than expected

There are a number of left join questions already, but still I can't quite put my finger on this issue. The WHERE condition doesn't look sensible to move.
The problem is that there should be 4 rows returned but only 1 is.
In checking the left join conditions, there is 1 row returned for each left join, which is correct for the number of records in the table, however the query below returns 1 record instead of 4, but I can't see how to return 4, yet.
Query follows: (Gives 1 result not 4; 4 being expected)
SELECT
list.uid,
list.business_uid,
list.creator_name,
business.company_name,
list_alias.uid AS list_alias_uid,
list_alias.alias AS list_alias,
list_member.uid AS list_member_uid,
mailbox.full_name AS list_member_name,
mailbox.email_address AS list_member_email_address
FROM
mailbox,
business,
list
LEFT JOIN
list_alias ON list_alias.list_uid=list.uid
LEFT JOIN
list_member ON list_member.list_uid=list.uid
WHERE
list.business_uid='1'
AND list.business_uid=business.uid
AND mailbox.uid=list_member.mailbox_uid
ORDER BY
list.full_name ASC
Data:
Business UID 1 has 4 lists
SELECT * FROM list WHERE business_uid=1 -- gives 4 results
SELECT * FROM list_alias WHERE list_uid IN (SELECT uid FROM list WHERE business_uid=1) -- gives 1 result
SELECT * FROM list_member WHERE list_uid IN (SELECT uid FROM list WHERE business_uid=1) -- gives 1 result
Any pointers on what I could check would be welcome.
Table Sample Data:
list:
uid | business_uid | creator_name | full_name
--------------------------------------------------
1 1 List Maker Subscribe to W
2 1 List Maker Subscribe to X
3 1 List Maker Subscribe to Y
4 1 List Maker Subscribe to Z
business:
uid | company_name
-------------------
1 List Company
list_alias:
uid | list_uid | alias
----------------------------------------
1 1 subscriber#list-url.com
list_member:
uid | list_uid | mailbox_uid
------------------------------------
1 1 1
mailbox:
uid | full_name | email_address
-------------------------------
1 I am He me#me.com
Try this using a single join methodology, like so.
SELECT list.uid,
list.business_uid,
list.creator_name,
b.company_name,
la.uid AS list_alias_uid,
la.alias AS list_alias,
lm.uid AS list_member_uid,
m.full_name AS list_member_name,
m.email_address AS list_member_email_address
FROM list LEFT JOIN list_member lm ON lm.list_uid=list.uid
LEFT JOIN mailbox m ON m.uid=lm.mailbox_uid
LEFT JOIN business b ON list.business_uid=b.uid
LEFT JOIN list_alias la ON la.list_uid=list.uid
WHERE list.business_uid=1
ORDER BY list.full_name ASC
Question: What are the values of 'uid' from the 'list' table? Because 'uid' is not the same as 'business_uid'. What I mean is ...
If the 'list' table has this ...
'uid' 'business_uid'
1 1
2 1
3 1
4 1
Then that is the problem. You are returning the same 'busines_uid' but a different 'uid' which means it will only match the first record.