How to do advanced data filtering across multiple fields?

Asked

Viewed 964 times

0

I have a table from which I need to get results varying by certain user data:

tabela

I need to return the rows where each column or hit with the respective user parameter or is empty (null). Consider as user parameters the bottom of the image - USER_ID=29, USER_GROUP=ADMIN, etc..

In the above case, return only Ids 30, 31, 33 and 36.

  • I ended up asking the question in a hurry, because I didn’t want to have to tell a whole story of my situation, and the problem was an illumination on the logic of filtering. I used a more generic situation possible to facilitate understanding. I confess that in your place, I wish to know more about it, for my help be the best possible for situation. Thank you all for your help! Unfortunately I can not upvote the reply of @Caffé, which was the one that solved my problem, very obvious :)

4 answers

3

The query below does exactly what is proposed in the statement:

SELECT *
FROM USER
WHERE (USER_ID = 29 OR USER_ID IS NULL)
AND (USER_GROUP = 'ADMIN' OR IFNULL(USER_GROUP, '') = '')
AND (find_in_set('1', USER_LEVEL) OR IFNULL(USER_LEVEL, '') = '')
AND (find_in_set('3', USER_TYPE) OR IFNULL(USER_TYPE, '') = '')
AND (find_in_set('10', USER_ROLE) OR IFNULL(USER_ROLE, '') = '')

You can check the correct return on Sqlfiddle.

  • 1

    In fact the function "find_in_set" does this trick of looking for an item in a comma-separated textual list. I had confused this function with the "in" itself in my reply but now corrected.

  • @Caffé Thanks for the tip, I forget that Mysql has so many facilities.

  • It worked perfectly! Too bad you can’t upvote :(

  • 1

    +1 for "playing it safe" (IFNULL) as @Zuul suggested.

2

This is a little weird and doesn’t have enough information but come on.

select * from tabela where user_id = $user_id || user_group = $user_group || find_in_set($user_level, user_level) || find_in_set($user_type, user_type) || find_in_set($user_role, user_role) || (user_id = null && user_group = null && user_level = null && user_type = null && user_role)

I put in the Github for future reference.

This works if these fields that may have more than one value have only one digit.

I interpreted the rules as I could visualize in the question. If this is not it you should make the rules clearer. You even said where this information comes from, I assumed again. I also assumed that the void would be null.

2


The query below returns the records where each field is either equal to the parameter or is null. The function find_in_set Mysql does the trick of searching for a value in a comma-separated text list.

select
    ID, USER_ID, USER_GROUP, USER_ROLE, USER_LEVEL, USER_TYPE
from
    tabela
where
    (USER_ID = 29 OR USER_ID is null)
    and (USER_GROUP = 'ADMIN' OR USER_GROUP is null)
    and (find_in_set(1, USER_LEVEL) OR USER_LEVEL is null)
    and (find_in_set(3, USER_TYPE) OR USER_TYPE is null)
    and (USER_ROLE = 10 OR USER_ROLE is null)

See working on sqlfiddle.

  • 1

    Which is why I don’t think the question is clear is that you’re assuming emptiness in the image posed in the question is in fact null in Mysql and not empty!

  • To be safe, it should be something like WHERE coluna IS NULL OR coluna = ''. But this is probably too much code if we know the table structure :)

  • I don’t know what @bigown’s comment you mean, but yours query is only correct if the fields are null, because if they are empty it fails. NULL is different from ''. But I’m just warning you, go ahead with your successful answer :)

  • 1

    Thank you, @Zuul. I am aware of this SQL detail. I’m actually assuming that worthless cells are NULL in the database, which is the most common practice.

  • It worked perfectly @Caffé! Too bad you can’t upvote :(

-2

I believe this is it, I will answer in PURE SQL

   SELECT * FROM nomeDaTable WHERE USER_ID = 29 OR USER_GROUP = "ADMIN" OR USER_LEVEL = 1 OR USER_TYPE = 3 OR USER_ROLE = 10 OR (USER_ID = "" && USER_GROUP = "" && USER_LEVEL = "" && USER_TYPE = "" && USER_ROLE = "")
  • 2

    The operator should not be used and in place of or?

  • 1

    That one query virtually returns everything from the database :)

  • I gave CTRL C the first piece and forgot to pack, vlw there

  • She won’t pick up line 36. Although by the apparent rules line 34 should also be picked up and he didn’t. If it’s not this, only he knows when to take a line or not.

  • Really, well observed, if you come to 36 should come to 34 too.

  • @Paulohdsousa 34 not because she has USER_TYPE = 2 and the filter asks USER_TYPE = or contains 3.

  • 1

    I don’t even know if these numbers are just examples. My interpretation is that it’s just an example because it wouldn’t make sense for someone to make a query in which one knows exactly what will return.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.