[英]Sql results into php array
I would like to create an array (in php) from sql results like this: We have the sql-table "Posts" which stores the Name and the Message .Example: 我想从像这样的sql结果创建一个数组(在php中):我们有一个sql-table“ Posts”,用于存储Name和Message 。例如:
Name |
姓名| Message
信息
John |
约翰| Hello
你好
Nick |
尼克| nice day
美好的一天
George |
乔治| Good bye
再见
John |
约翰| where
哪里
What i want is to output the names of people who have posted a message but dont display the same names more than 1 time . 我想要的是输出已发布消息但不显示相同名称超过1次的人员的姓名 。 So the output would be John,Nick,George.
因此输出将是John,Nick,George。 (From these records, we see that John has posted 2 messages, but at the final output, we see only one time his name).
(从这些记录中,我们看到John发布了2条消息,但是在最终输出中,我们只看到他的名字一次)。
Is this somehow possible? 这可能吗? Thanks in advance.
提前致谢。
Try: 尝试:
$sql = <<<END
SELECT DISTINCT Name FROM Posts
END;
$query = mysql_query($sql) or die($sql . ' - ' . mysql_error());
$names = array();
while ($row = mysql_fetch_array($query)) {
$names[] = $row[0];
}
print_r($names);
选择地区
You could run a SQL query to just select the distinct names, and nothing else: 您可以运行SQL查询以仅选择不同的名称,而无需执行其他操作:
SELECT DISTINCT Name FROM Posts;
This will give you a result set consisting of distinct Names values, with each unique value only being returned 1 time in the set. 这将为您提供一个由不同的Names值组成的结果集,每个唯一值在该集中仅返回1次。
要获得计数,您将需要使用group by进行汇总:
SELECT NAME , COUNT(*) as Posts FROM Posts GROUP BY NAME
Here is the SQL if you are not averse to group BY 如果您不喜欢按BY,这是SQL
select count(name) as N, name from posts group by name ;
People having more than 1 post 拥有1个以上职位的人
select count(name) as N, name from posts group by name having N > 1 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.