嗨我有一个PHP脚本从数据库获取数据我的问题是第1 - 50列的数据是相同的我想让它显示只有一次同名的数据
数据库就像这样
--------------------------------------------
Country | State | Name | Address|
--------------------------------------------
Japan | Ohio | Bill
Japan | Ohio | Anne
Japan | Ohio | Shyru
Japan | Nagoia| Ayusawa
Japan | Nagoia| Neji
Japan | Nagoia| Kaito
Japan | Osaka | Suwabara
Japan | Osaka | Kai
Japan | Osaka | Jane
我想获取数据俄亥俄州,Nagoia和大阪或任何可以在这里添加的州,但只会打印一次俄亥俄州
这是我的代码:
<?php
$con=mysqli_connect("127.0.0.1","root","","test");
if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$result = mysqli_query($con,"SELECT * FROM ftable ORDER BY Country");
while($row = mysqli_fetch_array($result))
{
echo $row['State'];
}
mysqli_close($con);
?>
此代码显示此
Ohio Ohio Ohio Nagoia Nagoia Nagoia Osaka Osaka Osaka
而且我只知道它只显示一次状态我怎么能这样做?并感谢你准备好了!任何帮助都会有所帮助
答案 0 :(得分:2)
添加group by子句
$result = mysqli_query($con,"SELECT * FROM ftable GROUP BY State ORDER BY Country");