N
N
Nikita Souvel2017-11-06 01:11:51
MySQL
Nikita Souvel, 2017-11-06 01:11:51

How can such a selection be made?

There is a base
Option 1.
ID - nesting - key
1 - 2 - type=2&count=12
2 - 1 - type=2
3 - 3 - type=5&count=12&meta=test
4 - 3 - type=2&count=12ℜ=direct
5 - 2 - type=2ℜ=direct
Variant 1.
page_id - key - value
1 - type - 2
1 - count - 12
2 - type - 2
3 - type - 5
3 - count - 12
3 - meta - test
4 - type - 2
4 - count - 12
4 - real - direct
5 - type - 2
5 - real -
direct

$search = array('type' => 2, 'count' => 12, 'real' => 'direct');

And how to make a query so that the result returns the id of the records 1, 2, 4, 5
i.e. all those where there are these elements but not others
I hope I clearly described the essence of the problem, if this is not enough, you can write what else needs to be described so that the essence of the issue is not clarified

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kamyannoy, 2017-11-06
@redfieldone

So, use a loop. Well, something like
$search = array('type' => 2, 'count' => 12, 'real' => 'direct');
foreach ($search as $value => $key) {
echo "{$value} => {$key} "; (do not be shy about parentheses, they are used to display a variable inside the echo)
}
Well, actually, work further with the received data.
P.S. If I understood correctly.

R
Rsa97, 2017-11-06
@Rsa97

SELECT `page_id`
  FROM `table`
  GROUP BY `page_id`
  HAVING COUNT(*) = SUM((`key` = 'type' AND `value` = '2') 
                         OR (`key` = 'count' AND `value` = '12') 
                         OR (`key` = 'real' AND `value` = 'direct'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question