S
S
soundie2021-12-10 03:06:25
MySQL
soundie, 2021-12-10 03:06:25

How to filter in the first table based on the data in the third (through the second)?

There are three tables:

Table1 (ID, Title)
Table2 (ID, Title, Table1.ID)
Table3 (ID, Title, Quantity, Table2.ID)

The first table is related to the second: 1 to ∞
The second table is related to the third: 1 to ∞

There are two tasks:

1. Display one value: the number of rows in Table1 , where in Table3 the value of the Quantity field will be greater than zero.

2. Display a table with two columns:
a) Table1.Name
b) Sum of field values ​​Table3.Quantity , which refer to each row of Table1 .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soundie, 2021-12-10
@soundie

I came up with the solutions myself (although I'm not completely sure):

SELECT Таблица1.Название, SUM(Таблица3.Количество) as 'count'
FROM Таблица1, Таблица2, Таблица3
WHERE (Таблица3.Таблица2_ИД = Таблица2.ИД) AND
      (Таблица2.Таблица1_ИД = Таблица1.ИД) AND
      count > 0
GROUP BY Таблица1.ИД

Second task:
SELECT Таблица1.Название, SUM(Таблица3.Количество)
FROM Таблица1, Таблица2, Таблица3
WHERE (Таблица3.Таблица2_ИД = Таблица2.ИД) AND 
      (Таблица2.Таблица1_ИД = Таблица1.ИД)
GROUP BY Таблица1.ИД

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question