0

This is an example of a string inserted into a mysql db field after a multiple choice checkbox form field has been submitted. (including the brackets): [1,0,1]

1=box checked

When the first number in the string = 1 value = pie, when the second number in this string =1 value= cookies, when the third number in this string =1 value = fruit. So the output for this string would be cookies, fruit.

I need to assign values to these numbers in an sql query. Something like:

select answer_data from answers where if the first number in the string = 1, the value is pie; if the 2nd number in the string = 1, the value is cookies;if the third number in the string=1, the value is fruit;

3
  • please take a look into stackoverflow.com/questions/3653462/… if tge number is fixed on entries , you can remove the brackest and use SUBSTING_INDEX to access all numbers, but that i a pain. Commented Nov 21, 2020 at 19:48
  • (1) Treat the string as a JSON array. (2) Create a dependency table (position - value) (3) Link by "$[I]" = i, group and select. Commented Nov 21, 2020 at 19:53
  • MySQL doesn't support checkboxes, which are a construct of some kind of application code. Commented Nov 22, 2020 at 9:25

1 Answer 1

1

Caveat: Storing the data in this implied mapping to an array definition is a very brittle design. A better approach would be to just store the actual text values as JSON.

However, if you must use that approach...

If you defined a stored procedure

You could use the Substring function to tease out the values - and assemble desired output

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

2 Comments

thanks for the help. I have an issue with changing the data type or entering the data in a different way - the db table in question is related to a third party plugin

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.