I have a database table that looks something like this:
User | Group
----------------
Bob | test1
Susy | test 1
Martha | test 2
Bill | test 3
Jake | test 3
I want to output all values from the group column, but repeat none. My goal output is this:
test1, test 2, test 3
Is there an easy way to do this via PHP?
You have plenty of ways to do this, either pure SQL or PHP. Here are three:
With GROUP BY
:
SELECT `Group` FROM Users GROUP BY `Group`
With DISTINCT:
SELECT DISTINCT `Group` FROM Users
Using array_unique() native php method
By the way, don't use reserved sql keywords as column or table names.
关键是SQL命令应包含DISTINCT关键字:
select distinct group from tableName
您可以将来自MySQL表的读取值导入到基于本地(php代码)的数组中,然后遍历该数组以查看是否已读取了要检查的值,如果没有,则回显。
Change the name of the field from Group
to something else or you might end up with problems
$data = mysql_query("Select distinct Group from table") or die(mysql_error());
while($row = mysql_fetch_array($data))
{
echo $row["Group"];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.