如何从带有重复的列的PHP SQL中创建唯一项的列表 [英] How to make a list of unique items from a column w/ repeats in PHP SQL
本文介绍了如何从带有重复的列的PHP SQL中创建唯一项的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个表,其中有一个列:
Say I have a table that has a column which goes:
Column B
apple
apple
apple
orange
apple
orange
orange
grapes
grapes
mango
mango
orange
我想以这样的方式查询它:
And I want to query it in such a way that I get a list like so:
苹果 橘子 葡萄 芒果
apple orange grapes mango
如何在PHP SQL中执行此操作?非常感谢.
How do I do this in PHP SQL? Much thanks.
推荐答案
假设您的表名为水果",而此列称为"B".这是您的操作方式:
Suppose your table is called 'Fruit' and this column is called 'B'. Here's how you would do it:
SELECT DISTINCT B FROM Fruit;
"DISTINCT"关键字将为您带来所有独特的结果. 那就是SQL部分.在PHP中,您可以这样编写查询:
The 'DISTINCT' keyword will get you all unique results. That's the SQL part. In PHP, you write the query like this:
// Perform Query
$query = 'SELECT DISTINCT B FROM Fruit';
$result = mysql_query($query);
// Get result
while ($row = mysql_fetch_array($result)) {
echo $row['B'];
}
这篇关于如何从带有重复的列的PHP SQL中创建唯一项的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文