I want to ask how can I check all rows in one column (which is foreign key) and then
if all that rows meet some condition then to get the result from it in MySQL.
For example I have three tables as follow
Products
ID Price
1 70
2 20
3 55
Bill
ID (others columns are not important)
100
200
300
ProductsBill
ID BillID ProductID
1 100 1
2 100 3
3 200 2
4 200 1
5 300 2
Now I want so select all BillsID where ALL products in that Bill has a price higher than 50,
which in this case should return the Bill with ID 100 because only this bill contains products which price are higher than 50
Thank you in advance.
Join the tables, group by billid and set the condition in the having clause:
select
pb.billid
from productsbill pb inner join products p
on p.id = pb.productid
group by pb.billid
having min(p.price) > 50
Try:
SELECT DISTINCT BillID FROM ProductsBill WHERE BillID NOT IN (
SELECT DISTINCT pb.BillID FROM
ProductsBill pb JOIN Products p on pb.ProductID = p.ID
WHERE p.PRICE < 50
);
The subquery ...
SELECT DISTINCT pb.BillID FROM
ProductsBill pb JOIN Products p on pb.ProductID = p.ID
WHERE p.PRICE < 50
... selects all BillID's where there is at least one product with an associated price < 50. The outer query then selects all BillId's that are in the ProductBill table that do not have at least one product with an associated price < 50.
What am trying to do is to return all the records where the total count of truck owners is more than the specified limit
that is in the case below suppose the total limit is 3 i would like to fetch all the owners who have 3 or more trucks registered
so the query if the limit is 3 should only return metro trans as they have 3 or more trucks
This is my db structure
tbl_trucks
id owner_id .....
1 12
2 12
3 13
4 12
tbl_truck_owners
id owner
12 metro trans
13 mush elec
How do i add the limit to my sql statement
THis is what i have tried
SELECT
COUNT(tbl_trucks.owner_id), tbl_truck_owners.owner
FROM tbl_trucks
LEFT JOIN tbl_truck_owners ON tbl_trucks.owner_id = tbl_truck_owners.id
WHERE //stuck at adding the limit
GROUP BY owner_id"
How do i add the limit
Use HAVING statement:
SELECT COUNT(tbl_trucks.owner_id), tbl_truck_owners.owner
FROM tbl_trucks
LEFT JOIN tbl_truck_owners ON tbl_trucks.owner_id = tbl_truck_owners.id
GROUP BY owner_id
HAVING COUNT(tbl_trucks.owner_id) >= 3
I have the following tables:
Table users
id name base_discount
1 jack 10
2 michael 20
3 richard 30
Table item
id name category_id price
1 hammer 1 10
2 knife 2 15
3 spoon 2 12
4 plate 3 20
5 tree 4 400
Table category
id name
1 tools
2 kitchen
3 dishes
4 garden
Table discount_category
id user_id category_id discount
1 1 1 20
2 1 3 25
3 3 3 10
4 1 2 15
Table discount_item
id user_id item_id discount
1 2 1 50
2 1 2 50
Now what I want to achieve. I want to attach the discount per item that a user has to the correct item. If that is not available (NULL) I want to attach the discount per category that a user has. And if that is not available (NULL), I want to attach the base discount that a user has. With the discount I then calculate the new price of the item. However, when I try using COALESCE() within SUM() I get a syntax error. What am I doing wrong?
Below is my current query:
SELECT item.id, item.name, category.id,
category.name AS category_name, item.price, SUM((100 -
COALESCE(
(
SELECT discount_item.discount
FROM discount_item
INNER JOIN users ON discount_item.user_id = users.id
WHERE users.id = '1' AND discount_item.item_id = item.id
),
(
SELECT discount_category.discount
FROM discount_category
INNER JOIN users ON discount_category.user_id = users.id
WHERE users.id = '1' AND discount_category.category_id = item.category_id
),
(
SELECT users.base_discount
FROM users
WHERE users.id = '1'
)
)) / 100 * item.price) AS new_price,
FROM item
INNER JOIN category ON item.category_id = category.id;
Please also see the below link for an SQL Fiddle (couldn't do it on sqlfiddle.com as it wouldn't load). In the example I have appended a suffix to each table name.
http://rextester.com/LCCKSD59098
You have an extra comma after new_price and before FROM ITEM, hence the error.
Rextester Demo
Do not select other columns in select if you are not using them in group by, as in other relational RDBMS, you will get error.
Also use alias for table names for better readibily and to avoid confusion.
I've table in MySQL with athletic performs.
Structure:
id (PRIMARY KEY)
athlete_id (FOREIGN KEY)
perform
category_id
discipline_id
If I want to select the best performs from this table (each athlete can be in results max once), I use this query:
SELECT
*
FROM
performs NATURAL
JOIN athletes
JOIN
(SELECT
athlete_id,
MIN(perform) AS perform,
category_id,
discipline_id
FROM
zaznamy
WHERE discipline_id = 4
AND category_id = 3
GROUP BY athlete_id) rec
ON performs.athlete_id = rec.athlete_id
AND performs.perform = rec.perform
AND performs.category_id = rec.category_id
AND performs.discipline_id = rec.discipline_id
ORDER BY performs.perform
LIMIT 25
I get right results. But I want to select the best performs from one discipline and more category together (e.g. Mens with Juniors etc). How do I use clause GROUP BY for two columns?
Maybe I don't understand something, but you can add two columns in group by, like the following example. Just add ,(comma) between the two or more columns.
SELECT
*
FROM
performs NATURAL
JOIN athletes
JOIN
(SELECT
athlete_id,
MIN(perform) AS perform,
category_id,
discipline_id
FROM
zaznamy
WHERE discipline_id = 4
GROUP BY athlete_id,category_id) rec
ON performs.athlete_id = rec.athlete_id
AND performs.perform = rec.perform
AND performs.category_id = rec.category_id
AND performs.discipline_id = rec.discipline_id
ORDER BY performs.perform
LIMIT 25 enter code here
EDIT - UPDATE
Ok, now I understand your problem. This is quite common, actually, where you want first to find the max value of a group and then request additional info on that value.
One way to achieve this is with a nested query as follow
SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
inner join
(SELECT
athlete_id, MIN(perform) AS perform
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id) tt
on tt.athlete_id = ss.athlete_id and ss.perform = tt.perform
The result is the one you described above.
If I use this query:
SELECT
athlete_id, MIN(perform) AS perform, category_id
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id, category_id
I get this results:
athlete_id perform category_id
1 11,14 1
1 11,54 3
1 11,54 5
1 10,71 7
2 11,04 1
2 11,24 3
2 11,54 5
2 12,14 7
3 10,94 1
4 10,94 1
4 11,34 3
But I need this result:
athlete_id perform category_id
1 10,71 7
2 11,04 1
3 10,94 1
4 10,94 1
For each athlete_id the best perform at once category_id.
This result will be JOIN with full table "perform".
SQL:
SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
INNER JOIN
(SELECT
athlete_id, MIN(perform) AS perform
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id) tt
ON tt.athlete_id = ss.athlete_id and ss.perform = tt.perform
Results:
athlete_id perform category_id
1 7,04 1
2 7,14 1
3 7,14 1
4 7,24 1
5 7,14 1
6 7,24 1
6 7,24 3
7 7,02 1
8 7,07 1
9 7,34 1
10 7,34 1
11 7,18 1
12 7,04 3
13 7,44 1
13 7,44 5
14 7,44 1
15 7,27 1
I´ve tried clause DISTINCT but results were same.
I have to count products inside each category like I have a category A that have two childs B and C.
In B category have 10 products and C category have 5 Products.
I want to write a query that count product like A = 15 B = 10 C = 5.
I have wrote a query that count only for child categories.
My query is given below
SELECT
c.`id`,
c.`parent_id`,
c.`name`,
c.`slug`,
c.`route_id`,
c.`description`,
c.`excerpt`,
c.`sequence`,
c.`image`,
c.`seo_title`,
c.`meta`,
c.`enabled`,
(SELECT
COUNT(`product_id`)
FROM
HM_category_products
WHERE
HM_category_products.category_id IN (SELECT
HM_categories.`id`
FROM
HM_categories
WHERE
(id = c.`id` OR id = c.`parent_id`))) AS count
FROM
`HM_categories` AS c
EDITED :- Below is my tables structure. This is just an example.
Category Table
id parent_id name slug enabled
1 0 Mobiles & Tablets mobiles-tablets 1
2 1 Mobile Phones mobile-phones 1
3 1 Tablets tablets 1
4 1 Mobile Accessories mobile-accessories 1
5 0 Electronics & Computers electronics-computers 1
6 5 Cameras & Accessories cameras-accessories 1
7 5 TV - Video - Audio tv-video-audio 1
Category Product Table
product_id category_id
1 2
2 2
3 2
4 2
5 3
6 2
7 3
8 3
9 3
10 2
11 3
12 2
13 2
14 2
15 2
You hav to use the "Group by" with something like:
SELECT
category_id, COUNT(*)
FROM
HM_category_products
GROUP BY category_id
This would get your table HM_category_products Grouped by category_id so will know how mane rows of each category its in the table, whats mean you will know the number of product of each category. Then you can join this resulting table with category one to get the category info. (Sorry for my english)
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns. SQL GROUP BY Statement
for more info abaut "Group by" read
12.16.2 GROUP BY Modifiers and
12.16.1 GROUP BY (Aggregate) Functions
EDIT: I see now what you whant. I already give you the direction but here its a step furder: DISCLAIMER: THIS QUERY ITS JUST EXAMPLE, IT DOSNT WORK
CREATE VIEW view_2 AS
SELECT
category_id, COUNT(*) AS product_sum
FROM
HM_category_products
GROUP BY category_id
CREATE VIEW view_1 AS
SELECT
*
FROM
HM_categories
LEFT JOIN
view_2 AS a ON HM_categories.id = a.category_id;
SELECT
id,
name,
(SELECT
SUM(product_sum)
FROM
view_1
WHERE
parent_id = final.id) as product_count
FROM
HM_categories AS final;
that way you will get the product of a parent category, your only missing a CASE on the last select "product_count" to the child categorys, but that sum its easy because you already hav it on the view_1 (again sorry for my english)