Write Several Numbers In A Field In The Database

0

I have a column in a table in the database that is called "synchronize" inside it I want to save several numbers separated by commas, and then do a check on a page as follows, that if there exists between those commas a number that is equal to the number passed in the URL does a function for me, how can I increment a new number with the comma in this field "sync" and how do I check between the commas if this number is equal to the last one in the URL ?? Thank you des de

    
asked by anonymous 26.04.2016 / 07:45

2 answers

2

Use JSON format field.

MySQL support for JSON format, which is perfect for your purpose.

Basically:

You will capture the numbers, put them in an array and convert them to JSON, after that it will save to the bank.

$numbers = [1,2,3];
$json = json_encode($numbers);
// save to db

Basically. MySQL from native support for field json, as you can refer to in the documentation:

link

After saving, just run the querys with JSON_CONTAINS to check if the number entered is present in the field.

See example of a query with JSON_CONTAINS

First I created a json and then asked if the 3 number was present in json.

See the documentation, implementation will be easy.

    
26.04.2016 / 15:11
1

First you must create a column that supports text. Although there are several numbers, the way you want to store it, it is impossible to use a numeric field.

  CREATE TABLE xxx (
        ....
        sincrozina VARCHAR(255) //defina o tamanho necessário
        ....);

If you want to add a value in this field, there needs to be a record (as you will create this, I do not know). To increment the field with a value, use the following SQL.

 UPDATE xxx SET sincroinza = CONCAT(sincroinza, ",", novo_número) WHERE ....

novo_numero is the new value you want to enter. To check if a number is in this list, you should do the following:

SELECT * FROM xxx WHERE sincroniza LIKE %numero_buscado%

Where numero_buscado is the number you want to check. If this query returns the record where the list of numbers is, it is because the value is in the list.

I just put SQL snippets. As you have not provided any code, and little information, you can not help with other implementation details, so you'll have to adapt these SQLs to your code to get the solution.

    
26.04.2016 / 08:20