K
K
Kirill Gorelov2020-04-20 14:46:05
MySQL
Kirill Gorelov, 2020-04-20 14:46:05

How to write SQL?

In short, I screwed up a little, now I need to come up with a crutch ... It is not possible to redo it in the next few months.

There is a table
5e9d8883995b9398701905.png

And now I need to make a query that filters eid by patronymic = Pavlovich and place_award = Kolomna
And it turns out that if I make a query, then both eid = 1 and eid = 18 can get into the output. only eid = 1.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2020-04-20
@Kirill-Gorelov

select eid
from t
where (key, value) in (('patronymic','Павлович'),('place_award','г.Коломна'))
group by eid
having count(eid) = 2

S
Sergey Pankov, 2020-04-20
@trapwalker

SELECT DISTINCT t1.eid
FROM
    t t1
    JOIN t t2 ON t2.eid = t1.eid AND t2.key = 'patronymic' AND t2.value='Павлович'
WHERE t1.key = 'place_award' AND t1.value = 'г.Коломна'

And learn to already provide initial examples of tables in the form of SQL, so that the answerers can quickly and easily test their solutions, and not write from their heads.

1
101-s, 2020-04-20
@101-s

you can set the sampling limit

SELECT * FROM table WHERE patronymic = Павлович AND place_award = г.Коломна LIMIT 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question