Home >Backend Development >PHP Tutorial >How to use the mysql function to convert the string '1,2,3' into '1', '2', '3'?

How to use the mysql function to convert the string '1,2,3' into '1', '2', '3'?

WBOY
WBOYOriginal
2016-09-14 09:41:211947browse

The data retrieved from the database is a string like this:'1,2,3'
The sql I want to write now is like this: select * from a where id in('1','2',' 3');
How to realize that '1,2,3' becomes '1','2','3'?
Don’t use a foreach loop and query one record each time.

Reply content:

The data retrieved from the database is a string like this:'1,2,3'
The sql I want to write now is like this: select * from a where id in('1','2',' 3');
How to realize that '1,2,3' becomes '1','2','3'?
Don’t use a foreach loop and query one record each time.

This sql can be executed correctly: select * from a where id in (1,2,3);
So you can just splice the sql directly.
$str = '('.'1,2,3 '.')';
$sql = select * from a where id in $str;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn