T
T
TANK_IST2018-11-06 01:42:53
MySQL
TANK_IST, 2018-11-06 01:42:53

How to make such sql query from two pages?

There are two tables, one list with parameters, the second list with values
5be0c5fd5f98b625437290.png5be0c60b98001335697088.png
​​You need to make a selection by the filter name = the value of the
filters can be several
Compiled such a query

SELECT P.post FROM fields_post AS P, fields AS F
WHERE (P.value = 'Medium' AND F.name = 'Size')
AND (P.value = 'Purebred' AND F.name = 'Type')
AND P.field = F.id

But it doesn't work with multiple filters.
How to rewrite it so that the selection works?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-11-06
@Stalker_RED

It's not entirely clear what exactly you are doing, but perhaps you wanted something like this:

SELECT P.post FROM fields_post AS P, fields AS F
WHERE P.field = F.id
AND (
  (P.value = 'Medium' AND F.name = 'Size')
  OR -- это важно
  (P.value = 'Purebred' AND F.name = 'Type')
)

Or how do you want it to work if you have P.value = 'Medium' and P.value = 'Purebred' at the same time?

N
nrv, 2018-11-06
@nrv

1) Personal feeling: please do not join the table in where. If you join them, write a normal join (like t1 join t2 on t1.col = t2.col). Practice shows that those who match tables in where do not understand what they are doing.
2) Information is not enough. Are you sure you need to join by P.field = F.id? You need a normal description of what the tables are.
3) Several conditions - what is this? And for the first one to come out (record(s) 'Medium' + 'Size') and the second one (record(s) 'Purebred' + 'Type') at the same time? Then ((P.value = 'Medium' AND F.name = 'Size')
or (P.value = 'Purebred' AND F.name = 'Type'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question