Count rows in table with certain value in other table - mysql

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

Related

With SQL how to select rows that the joined table matches 2 values

If we have a table, orders:
Order ID
Order Table
1
100
1
50
And we have a table, OrderProducts:
OrderID
Product ID
ProductName
ProductType
1
1
ProductOne
Small
1
2
ProductTwo
Big
1
3
ProductThree
Small
2
4
ProductFour
Big
2
5
ProductFive
Big
How do I use SQL to return only the Orders that contain products of 2 specific types. In the scenario above I want to return only products with ProductType = 'Small' and 'Big' but ignore orders like "2" which only have products of two 'Big' on.
Results:
Order ID
1
I have tried creating a view to help, inner joins with multiple clauses but I am struggling. I am using MySQL for this and think my logic is simply "out".
Any advice is appreciated.
Aggregation is one method:
select order_id
from orderproducts op
where ProductType in ('Small', 'Big')
group by order_id
having count(distinct ProductType) = 2;

SQL query to return items with count sale_id = 4

I have 3 tables:
user: user_id | user_name
Sale: sale_id(pk) | user_id(fk) | sale_amount
item: item_id(pk) | sale_id(fk)
The sale_id is generated everytime a user purchases from the item table.
I wish to generate an output that will written me user_id, sale_id and count(item_id) i.e, total items purchased by a user in separate transaction where count(sale_id) in item table =4.
I tried using this but it does not work:
select s.user_id,s.id, count(i.item_id)
from sale s join item i
on s.id = i.sale_id
group by s.use_id, i.sale_id
having count(i.sale_id) = 4
;
If a user_id 1 as 2 different transactions where in first transaction(sale_id = 3) he buys 2 items and in second transaction (sale_id = 13) he purchases 4 items which means in the item table each four items purchased will have same sale_id. So, I want to see the output where users have purchased 4 items in one transaction.
output:
s.user_id | i.sale_id | count(i.item_id)
1 13 4
you have aggregation with respect to s.user_id,s.id however you are grouping by s.user_id, i.sale_id. This typically causes error because your query is malformed.
furthermore if you put i.sale_id into group by and try to use having with count(i.sale_id) = 4 this never returns results. because count(i.sale_id) will always be either 0 or 1 when you group by with sale_id

How to use having count less than another table

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

MySQL query - get row with highest number filtered by another column

I have a table with data similar to this:
id job_id filename status revision product
----------------------------------------------------------
1 1 1r0.jpg P 0 Product 1
2 1 2r0.jpg P 0 Product 2
3 1 3r0.jpg P 0 Product 3
4 1 2r1.jpg P 1 Product 2
I want to run an SQL query that returns the following:
id job_id filename status revision product
----------------------------------------------------------
1 1 1r0.jpg P 0 Product 1
3 1 3r0.jpg P 0 Product 3
4 1 2r1.jpg P 1 Product 2
i.e. if there's two rows with the same product name, I want to get the one with the highest revision number.
I've tried lots of queries using groupby, max, distinct to no avail. I've tried self-joins but cannot get the results I'm after.
For example, the query SELECT *, MAX(revision) FROM artworks GROUP BY product gives me data that looks fine at first glance but the row contains the data from the one with the lowest revision number, not the highest.
You can do it without GROUP BY, like this:
SELECT *
FROM artworks a
WHERE NOT EXISTS (
SELECT *
FROM artworks aa
WHERE a.product=aa.product AND a.revision < aa.revision
)
You may get the same result without a sub-query:
SELECT a.*
FROM artworks a
left join artworks b
on a.produc = b.produc
and a.revision < b.revision
where b.id is null;

Querying SQL data using three tables

I am trying to retrieve the CategoryID and CategoryName by seeing the CategoryBusinessMapping and Review Rating table. I am trying to retrieve the data of following Category table:
Category ParentCategoryID CategoryName
1 null Education
2 1 School
3 null Health
4 3 Doctors
5 1 Colleges
I have the Business table which has BusinessID and BusinessName and BusinessDescription like this:
BusinessID BusinessName BusinessDescription
YP00001 XYZ ABCD
YP00002 ABC XYZA
I have the CategoryBusinessMapping table like this:
MappingID CategoryID BusinessID
1 1 YP00001
2 2 YP00001
3 5 YP00001
4 3 YP00002
5 4 YP00002
I have this mapping table to map the different Category with the Business. I also have the Rating table like this:
RatingID BusinessID
1 YP00001
2 YP00001
3 YP00001
4 YP00002
5 YP00002
Here in this table I am assuming that a record having same BusinessID is fall under most popular Business. Meaning, here in above the Business ABCD having ID = YP00001 has four records in Rating table. Therefore it falls under most popular Business. Similarly YP00002 falls next to YP00001. By seeing the most popular Business in descending order I want to retrieve CategoryName and CategoryID. I have tried this to retrieve from the Rating table only:
select Distinct ReviewRating.BusinessID
,Count(*)as Rating
from YP.utblYPReviewRatingDtls as ReviewRating
group by ReviewRating.BusinessID
order by Rating desc
I have tried this:
SELECT distinct c.CategoryName, b.BusinessID
FROM Category c
INNER JOIN categoryBusinessMapping cbm
ON (c.CategoryID=cbm.CategoryID)
INNER JOIN Business b
ON (cbm.BusinessID=b.BusinessID)
LEFT JOIN Rating r
ON (cbm.BusinessID=r.BusinessID)
where c.ParentCategoryID is null
but I get the result which is redundant. I also remove the BusinessID from the query and I get the result but the result is incorrect. How can I remove redundancy and also get the proper output?
Use join and take the count of BusinessID from rating table and order your results
SELECT c.*, COUNT(r.BusinessID) AS bcount FROM Category c
INNER JOIN CategoryBusinessMapping cbm ON (c.Category=cbm.CategoryID)
INNER JOIN Business b ON (cbm.BusinessID=b.BusinessID)
LEFT JOIN Rating r ON (cbm.BusinessID=r.BusinessID)
GROUP BY r.BusinessID
ORDER BY bcount DESC