Ausy
Ausy

Reputation: 41

matching comma separated string to a database field and sort the result to be in the same order as the comma separated string

I have a variable with comma separated string, so I'm matching them with the values of another table field, how do I sort the result by the order in which the comma separated strings are structured. thank you currently I'm doing this.

$email_address = [email protected],[email protected],[email protected],; 
 
$sql ="SELECT id,company_ref,name FROM  employee WHERE email_address IN ('".str_replace(",", "','",    $email_address)."')";
 
$result = mysqli_query($db_conx, $sql);
$total = mysqli_num_rows($result);
   

Upvotes: 0

Views: 141

Answers (1)

Rikesh
Rikesh

Reputation: 26431

You can use ORDER BY FIELD

ORDER BY field(email_address, '[email protected]','[email protected]','[email protected]');

Reference: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field

Upvotes: 1

Related Questions