我有一个数据库和1个table.table名称是:cranetype他们有4个字段(cf_did,cf_firstname,cf_comment,cf_cranetype).
数据库值如下.
cf_did cf_firstname cf_comment cf_cranetype
1 Alexy tfhgfnjh 2,3
2 Thomas fdghfgh 11,6,3
3 Thomas cgjkjhl 5,6,11,3
4 Thomasxc cgjkjhl 1,6,9,4
5 Thomaseg fdghgh 11,12,3
6 Thomasm fsgdfgbd 11,6,3
7 Thomabs dgtrty 7,9,11
8 Rdgfghfdg bfdhh 1,3,4
9 Gngfdytyt eertret 1,6,3
10 Dvbgfj hfdhhyrtyr 6,3
11 Hnfhghyt bfgfdhg 11,6,3
12 Adgfdyh dfsdtrst 1
13 Rida gfhgfjgh 3,7,68
我有一个选择框,它们有值(1,2,3,4,…).
如果我从选择框中选择值1.
我想检查数据库表(cranetype)字段cf_cranetype.
例如:
$sql= mysql_query("SELECT * FROM `cranetype` WHERE `cf_cranetype`='1'");
while($row=mysql_fetch_array($sql))
{
<?Php echo $row['cf_did'];?>
}
我想要这样的输出:
if cf_cranetype =1.
4,8,9,12. (these are cf_did)
我的代码是:
$cranetype = $_POST['cranetype'];
$words = explode(',', $cranetype);
if (strlen($cranetype) > 0) {
$Where = '';
foreach ($words as $Item) {
$Where = "cf_cranetype LIKE '%$Item%' AND";
}
$Where = substr($Where, 0, -4);
$list_ul = "(SELECT * FROM cf_directory` WHERE $Where)";
$query = mysql_query($list_ul) or die(mysql_error());
}
最佳答案 解决方案是使用find_in_set和group_concat:
SELECT GROUP_CONCAT(cf_did) as cf_dids
FROM `cranetype`
WHERE FIND_IN_SET('1', `cf_cranetype`) > 0;
演示:
create table cranetype(cf_did int, cf_firstname varchar(100), cf_comment varchar(100), cf_cranetype varchar(50));
insert into cranetype values
(4, 'Thomasxc', 'cgjkjhl', '1,6,9,4'),
(7, 'Thomabs', 'dgtrty', '7,9,11'),
(8, 'Rdgfghfdg', 'bfdhh', '1,3,4'),
(9, 'Gngfdytyt', 'eertret', '1,6,3'),
(12, 'Adgfdyh', 'dfsdtrst', '1');
SELECT GROUP_CONCAT(cf_did) as cf_dids
FROM `cranetype`
WHERE FIND_IN_SET('1', `cf_cranetype`) > 0;
输出:
mysql> SELECT GROUP_CONCAT(cf_did) as cf_dids
-> FROM `cranetype`
-> WHERE FIND_IN_SET('1', `cf_cranetype`) > 0;
+----------+
| cf_dids |
+----------+
| 4,8,9,12 |
+----------+
1 row in set (0.00 sec)