我有20行,一行一臺具有例如:如何從MySQL錶行中選擇唯一的數組?
2,3,5,6,8,22
2,3,5,6,8,22,44,55
etc.
如何從MySQL錶行只選擇唯一的號碼,不重複這樣的結果是:
2,3,5,6,8,22,44,55
表定義:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL auto_increment,
`active` tinyint(1) NOT NULL default '1',
`facilities` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
INSERT INTO `test` (`id`, `active`, `facilities`) VALUES
(1, 1, '1,3,5,6,7,8'),
(2, 1, '2,3,4,5,8,9'),
(3, 1, '4,5,6,7,9,10');
這裏是我的嘗試:
SELECT DISTINCT facilities FROM test WHERE active='1'
$dbgeneral= explode(',', $row['facilities']);
$facilities = array(
"Air Conditioning" => "2",
"Balcony" => "4");
foreach ($facilities as $facilities=> $v) {
if(in_array($v,$dbgeneral)) {
echo '';
}
}
這是一個用逗號分隔的數字嗎? – 2010-08-05 17:29:52
你可以顯示錶定義?因爲阿拉坦的獨特答案可能是正確的,但是你的每個「行」是否只包含1個字段?或者是他們的多個領域? – Doon 2010-08-05 17:30:20
這裏是我的表: ID 2 - 3 - 5 \t 設施 1,2,3,4,5,8,9 - 1,7,9,11,13 - 4,5,6 ,9,10 – Stipe 2010-08-05 17:53:23