如何使用PHP和MYSQL从列中仅检索唯一值?
这是table1的内容:
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();
那么,你们觉得怎么样?这让我发疯了!
肌氨酸
答案 0 :(得分:0)
试试这个:
SELECT DISTINCT TRIM(value) FROM table1
答案 1 :(得分:0)
试试这个
$rs = mysql_query("SELECT DISTINCT value FROM table1", $dblink);