I have the following Tables :
Table : product
--------------------------
id_product | name_product
--------------------------
1 | p1
2 | p2
3 | p3
Table : number
-----------------------
imei | id_product
-----------------------
56789 | 1
56799 | 2
56713 | 3
Table : Operatio
----------------------
imei | date
------------------
56789 |31-07-2017
At last I want to get this result :
---------------------------------------
name_product | number_operation
---------------------------------------
p1 | 1
p2 | 0
p3 | 0
Thanks You
This will count the number of operations by product:
select name_product, sum(num_op)
from product p1
left join number n2
on n2.id_product = p1.id_product
left join
(
select imei, count(operation) as num_op
from Operation
group by imei
) x
on x.imei = n2.imei
group by name_product
For the number of products that have had an operation:
select name_product, count(distinct o3.imei)
from product p1
left join number n2
on n2.id_product = p1.id_product
left join Operation o3
on o3.imei = n2.imei
group by name_product
A possible solution :
SELECT name_product, COUNT(*)
FROM product p
LEFT JOIN number n ON n.id_product = p.id_product
LEFT JOIN operation o ON o.imei = n.imei
GROUP BY p.id_product
SELECT product.id_product,COUNT(number.id_product)
FROM product
INNER JOIN number ON product.id_product = number.id_product;
GROUP BY product.id_product
SELECT name_product, count (imei) c
FROM (SELECT name_product, o.imei
FROM (SELECT p.name_product, n.imei
FROM product p, "number" n
WHERE p.id_product = n.id_product) t
LEFT JOIN
Operatio o
ON t.imei = o.imei)
GROUP BY name_product
Try this code
Related
How do i List the CUSTNUMs and NAMES of any customer who has only ordered chemical [NUMBER].
ORDERS TABLE
+---------+--------+------------+------+
| CUSTNUM | CHEMNO | DATE | QTY |
+---------+--------+------------+------+
| 123456 | 1234 | 2000-00-00 | 35 |
+---------+--------+------------+------+
CUSTOMER TABLE
+---------+-----------+-----------+
| CUSTNUM | NAME | LOCATION |
+---------+-----------+-----------+
| 123456 | AmChem | New York |
+---------+-----------+-----------+
You could join the CUSTOMER and ORDERS tables containing orders for a particular <chemno> with a subquery for the custnum that buy only a product:
SELECT
CUSTNUM, NAME
FROM
CUSTOMER c
INNER JOIN
ORDERS o ON o.CUSTNUM = c.CUSTNUM and o.CHEMNO = <chemno>
INNER JOIN
( SELECT
CUSTNUM
FROM
ORDERS
GROUP BY
CUSTNUM
HAVING
COUNT(DISTINCT CHEMNO) = 1 ) t ON t.CUSTNUM = o.CUSTNUM
I will approach this with one join between both tables, then grouping by the column CUSTNUM of the ORDERS table and finally adding the required conditions on the HAVING clause, like this:
SELECT
o.CUSTNUM,
c.NAME
FROM
ORDERS AS o
INNER JOIN
CUSTOMER AS c ON c.CUSTNUM = o.CUSTNUM
GROUP BY
o.CUSTNUM
HAVING
( COUNT(DISTINCT o.CHEMNO) = 1 AND MIN(o.CHEMNO) = <some_chemno> )
OK, slow day...
SELECT DISTINCT x.custnum
FROM orders x
LEFT
JOIN orders y
ON y.custnum = x.custnum
AND y.chemno <> x.chemno
WHERE x.chemno = 9377
AND y.order_id IS NULL;
The rest of this task has been left as an exercise for the reader
I have the following tables
TABLE: appointments
ID | PRICE | PAID
48 | 100 | 180
TABLE: appointments_products
ID | APPOINTMENT_ID | PRODUCT_ID | TOTAL
10 | 48 | 1 | 30
11 | 48 | 9 | 30
12 | 48 | 6 | 30
I Would like to somehow run a MySQL query that will:
a) join the two tables, SUM the "TOTAL" of appointments_products for each appointment_id and if the "PAID" is not equal of the PRICE (from appointments table) + TOTAL (from appointments_products table) then to show it.
This is what I have done so far:
select a.*, b.appointment_id as AppId, b.total as ProdTotal
from appointments a
INNER JOIN appointments_products b ON a.id = b.appointment_id
But this query does not sum the total for each appointment_id
select a.ID,a.PRICE,a.PAID,a.id as AppId,
sum(b.total) as ProdTotal
from appointments a
INNER JOIN appointments_products b ON a.id = b.appointment_id
group by a.ID,a.PRICE,a.PAID;
Use where to check if price is equal to paid and the use group by to group with appointment_id.
select b.Appointment_Id, a.price, a.PAID, a.id, sum(b.total) AS TotalProd FROM appointments_products AS b inner join appointments as a On Appointment_Id = a.Id group by Appointment_Id, a.Price , a.PAID , a.id HAVING a.PAID != (a.Price + sum(b.Total))
It's possible left join only one row without sub query?
I need to get product statistics and some of products have multiple groups.
Therefore, the amount of products is incorrect.
SELECT COUNT(p.id) AS total_product, SUM(p.price) AS total_price
FROM product p
LEFT JOIN attribute_group a ON
a.product_id = p.id
WHERE p.created_at >= "2018-01-01" AND (a.id = 1 OR a.id = 2)
GROUP BY p.id
LIMIT 0, 30;
product
id | price
1 | 100
2 | 150
3 | 250
attribute_group
id | product_id | title
1 | 1 | a1
2 | 1 | a2
3 | 2 | a3
4 | 3 | a4
Should be:
1| 100
But i get:
2 | 200
You appear to want all products or the counts/sum of them that have attributes of both 1 and 2. Here is one method:
SELECT COUNT(*) as num_products, SUM(p.price) as total_price
FROM product p
WHERE p.created_at >= '2018-01-01' AND
EXISTS (SELECT 1
FROM attribute_group ag
WHERE ag.product_id = p.id AND ag.id = 1
) AND
EXISTS (SELECT 1
FROM attribute_group ag
WHERE ag.product_id = p.id AND ag.id = 2
);
I am trying to count the number of results in a column after joining a table and I am having a hard time getting my query to work.
In the end result, I need to get a table with the product id, product name and the number of medias for each product.
This is what I have so far:
SQL
select
p.id,
p.name,
count(distinct mp.media_id)
from products as p
left join medias_products as mp
on mp.product_id = p.id
group by mp.media_id
order by p.id
These are the tables:
Medias
Id | client id
------ | ---------
1 | 1
2 | 2
Products
id | name | client_id
------ | -------- | ---------
1 | product1 | 1
2 | product2 | 2
medias_products
product_id | media_id
---------- | --------
1 | 2
2 | 1
Client
id | name
------ | -----
1 | Peter
2 | John
In addition, I'd like to find another query that would give me the results filtered by an specific client id.
Can someone please shed some light and share the knowledge.
Thanks in advance.
Try this:
select
p.id,
min(p.name) as name,
count(distinct mp.media_id) as medias
from products as p
left join medias_products as mp
on mp.product_id = p.id
group by p.id
order by p.id
For your second query:
select
p.id,
min(p.name) as name,
count(distinct mp.media_id) as medias
from products as p
inner join medias_products as mp
on mp.product_id = p.id
inner join medias as m
on m.id = mp.media_id
inner join clients as c
on c.id = m.client_id
where c.id = <your client's id>
group by p.id
order by p.id
Shouldn't you GROUP BY p.id instead of mp.media_id? That is
select
p.id,
p.name,
count(distinct mp.media_id)
from
products as p
left join medias_products as mp
on p.id = mp.product_id
group by p.id
order by p.id
I have three tables like below.
Courses:
Id Name
1 A
2 B
3 C
4 D
Prices:
Id CId Price
1 1 200
2 2 150
3 3 500
4 4 300
Payment:
Id CId
1 1
2 4
If a record of payment for Course record exists, then Price should be -1. I want to get result like below:
Id Name Price
1 A -1
2 B 150
3 C 500
4 D -1
How should I query mysql?
Try This
SELECT c.Id, c.name,
CASE WHEN pa.id IS NOT NULL THEN -1 ELSE pr.price
END as Price
FROM Courses c
LEFT JOIN Prices pr
ON c.Id = pr.CId
LEFT JOIN Payment pa
ON c.id = pa.CId;
OR
SELECT c.Id, c.name,
CASE WHEN pa.id IS NULL THEN pr.price ELSE -1
END as Price
FROM Courses c
LEFT JOIN Prices pr
ON c.Id = pr.CId
LEFT JOIN Payment pa
ON c.id = pa.CId;
Working code:
select c.id, c.Name, IF(pm.CId is null, p.Price, -1) as Price
from Prices p left join Payment pm
on p.CId = pm.CId
inner join Courses c
on p.CId = c.id
order by c.id
SqlFiddle: http://sqlfiddle.com/#!9/f1b715/3
SQL Fiddle Demo Thanks to #Barmar
SELECT C.ID, C.NAME, CASE WHEN pay.CId IS NULL THEN P.PRICE
ELSE -1
END as Price
FROM Courses C
JOIN Prices P
ON C.Id = P.CId
LEFT JOIN Payment pay
ON C.id = pay.CId
ORDER BY P.id
OUTPUT
| Id | Name | Price |
|----|------|-------|
| 1 | A | -1 |
| 2 | B | 150 |
| 3 | C | 500 |
| 4 | D | -1 |
SELECT p.id,c.NAME, CASE WHEN p.cid=e.cid THEN -1 ELSE p.price END AS price
FROM prices p
LEFT JOIN courses c
ON c.id=p.ID
LEFT JOIN payment e
ON p.cid=e.cid