1

I have a table invoices:

    id|orders|user|status|
    1 |1,2,5 |1   |1     |
    2 |75    |2   |0     |
    3 |31,4  |5   |1     |

What should be my request to the database so that I receive the invoice status by the order number in the orders column.

It seems to me that the way to use LIKE here is not suitable as it will select incorrect values.

    $sql = "SELECT status FROM invoices WHERE orders LIKE '%".$order_id."%'";
Vidal
  • 2,605
  • 2
  • 16
  • 32
user3514052
  • 418
  • 4
  • 18

2 Answers2

1

What you want is

 $sql = "SELECT status 
         FROM invoices 
         WHERE CONCAT(',', orders, ',') LIKE '%,".$order_id.",%'";
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
0

You can try this assuming no space is used

$sql =<<<REQQ
SELECT status 
FROM invoices 
WHERE orders LIKE '{$order_id}'
OR orders LIKE '{$order_id},%'
OR orders LIKE '%,{$order_id},%'
OR orders LIKE '%,{$order_id}'
REQQ;
//end
Florent
  • 436
  • 3
  • 8