如何使用PHP和MYSQL从列中仅检索唯一值?
这是表1的内容:
id value
_____________________
1 Max Power
2 Homer Simpson
3 Max Power
4 Lisa Simpson
5 Lisa Simpson
所以,显然,我想要这些结果:
- Max Power
- Homer Simpson
- Lisa Simpson
但我仍然得到:
- Max Power
- Homer Simpson
- Max Power
- Lisa Simpson
- Lisa Simpson
我尝试过的SQL语句如下:
SELECT DISTINCT value FROM column1
和
SELECT * FROM column1 GROUP BY value
显然这些应该起作用。因此,这是我的完整SQL语句,以防万一其中有些可疑之处:
<?php
$dblink = mysql_connect(‘host’, ‘username’, ‘password’) or die( mysql_error() );
mysql_select_db(‘database_name);
$rs = mysql_query('SELECT DISTINCT value FROM table1, $dblink);
while($row = mysql_fetch_array($rs)) {
$rowValue = $row['value'];
$explodeRowValue = (explode("||",$rowValue));
foreach ($explodeRowValue as $value_name) {
$data[] = array(
'key' => $value_name,
'value' => $value_name
);
}
}
echo json_encode($data);
flush();
那么,你们怎么看?这让我疯狂!
萨尔
最佳答案
尝试这个:
SELECT DISTINCT TRIM(value) FROM table1