D
D
Daniil Gorbenko2021-04-13 12:39:04
JavaScript
Daniil Gorbenko, 2021-04-13 12:39:04

How to create a SQL query to get data on multiple rows?

I have 2 tables. Table 1 contains information about products in the form:

|id___|__name___________|
|1 ___|__samsung 1207_|
|2 ___|__nokia 5537_____|
|3 ___|__LG-3124________|

The second table contains price information in the format:

id_goods_|_price__|__unit__|__date
1 ________|_140___|__eur___|01-01-2020
1 ________|_150___|__eur___|01-01-2021
2 ________|_200___|__eur___|01-01 -2019
2 ________|_190___|__eur___|01-01-2020
2 ________|_205___|__eur___|01-01-2021
3 ________|_205___|__eur___|01-01-2021

It turns out that the Samsung costs 140 euros in 2020 and 150 in 2021.
I need to get data from the second table where the prices of goods were once equal to well-defined values. For example, I want to receive goods that once cost 140 dollars and 150 euros, then I should only have samsung displayed. If I want to receive goods that once cost 200 and 205 euros, then I should withdraw only Nokia (since LG never costs $200, it should not be withdrawn).
Please tell me how to build a SQL query in this format.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kudis, 2018-06-25
@kudis

1. to get a json response from ajax , you need to politely ask by adding the parameter dataType : 'json' this is an internal kitchen and the user only needs to know that the form did not submit and he needs to "wait and repeat"

S
Slava Rozhnev, 2021-04-13
@daniilgorbenko

You can use the following query:

select g.id, g.name, count(distinct price)
from goods g
join prices p on g.id = p.id_goods
where price in (200, 205)  -- prices that we need to search
group by  g.id, g.name
having count(distinct price) = 2; -- 2 count different prices

MariaDB fiddle

A
Alexey Tsarapkin, 2021-04-13
@Dreamka

Use JOINs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question