0

I have a table which has a field called tags which is of type varchar. this column holds json_encoded data.

["4","5","6","7"]

a record can have multiple tags. I don't wanna to resort to using pivot table which I know is more efficient. I want to keep it this way at the moment.

i want to filter all the records which has specific tag. just a single tag... How can i create a query for that?

4
  • 1
    What you have try so far?.. Add some code here... Commented Jun 16, 2016 at 5:24
  • $cats = $_POST['cats']; if(! $cats==0) { $filters .= 'AND category` LIKE %'. $cats . '%'; }` Commented Jun 16, 2016 at 5:28
  • This code is invalid, could that be your problem? The query itself looks good enough to return any tags that is a permutation of $cats Commented Jun 16, 2016 at 5:31
  • yeah it just lacks double quotes before and after % Commented Jun 16, 2016 at 5:49

1 Answer 1

1

SELECT * FROM WHERE json_encoded_list_of_tags LIKE '%TAG%'

Assuming you only need the one tag the %'s are wildcards (as you probably know?) and this will match any row that has TAG in the column.

you should also refine this query to include escaped quotation marks, to avoid matching with substrings of other tags

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.