Select record that contains result equivalent to a variable

1

I have a column in my mysql table, where it is called "positions", where several values in int are placed there, however, with the "|" of division.

Example: 8 | 19 | 10 | 20 |

And each number represents a category.

I would like to do a SELECT where I having only a number, would select the records that had that number in the middle.

Example: I chose 8 to make the select in the table, where my column positions is equal to 8 | 19 | 10 | 20 |, as it has the number 8, it will return positively .

<?php
$id = isset($_GET['id']) ? $_GET['id'] : ''; // numero que vou selecionar
$sql = mysql_query("SELECT * FROM usuarios WHERE cargos LIKE '%$id%' OR cargos = '$id' ORDER BY id");
while($ver = mysql_fetch_array($sql)){

?>

I tried to do it this way, but it did not work out with the "LIKE".

    
asked by anonymous 05.10.2017 / 18:15

1 answer

0

As our friend @Inkeliz mentioned, the ideal is to reformulate the database and create a table to associate the post with the user. But if you can not do this rework, you can dodge this problem as follows:

$sql = mysql_query("SELECT * FROM usuarios WHERE (cargos LIKE '%|$id|%' OR cargos LIKE '%|$id' OR cargos LIKE '$id|%') ORDER BY id");

The problem with doing this is that you will lose the speed of the query.

    
05.10.2017 / 19:00