我实际上只需要一个简单的查询,
以下是我的表格:
信息 - > id_info,year,information_name,person_name,country_id,state_id,city_id
国家/地区 - > id,country_id,country_name
州 - > id,state_id,country_id,state_name
城市 - > id,city_id,state_id,city_name
我已经运行了一些查询,例如' s:
SELECT *
FROM information, country, state, city
WHERE information.country_id =country.country_id
AND information.state_id = state.state_id
AND information.city_id = city.city_id
GROUP BY information.id_info;
我的简单脚本:
echo '<td>'.$data['country_name'].'</td>
echo '<td>'.$data['state_name'].'</td>
echo '<td>'.$data['city_name'].'</td>
$data-> Is my while script with $query=mysql_query...
从上面的查询中只显示我数据库中的两(2)个数据,但在数据库中它有五(5)个数据。
然后我试图删除Group语句,但数据保持循环并显示近8000个数据,但我只有5个数据表。
我已尝试过一切,左连接,右连接,内联......
我需要帮助,我知道这很简单,但我怎样才能正常显示所有数据。
感谢。
此处显示数据的完整脚本:
<table cellpadding="5" cellspacing="0" border="1">
<tr bgcolor="#CCCCCC">
<th>No.</th>
<th>Year</th>
<th>Information Name</th>
<th>Person Name</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Act</th>
</tr>
<?php
include('connect.php');
$query = mysql_query("SELECT *
FROM information
INNER JOIN country ON information.country_id =country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
GROUP BY information.country_id, information.state_id, information.city_id") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo '<tr><td colspan="6">No data!</td></tr>';
}else{
$no = 1;
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$data['year'].'</td>';
echo '<td>'.$data['information_name'].'</td>';
echo '<td>'.$data['person_name'].'</td>';
echo '<td>'.$data['country_name'].'</td>';
echo '<td>'.$data['state_name'].'</td>';
echo '<td>'.$data['city_name'].'</td>';
echo '<td><a href="viewinfo.php?id='.$data['id_info'].'">Detail</a> / <a href="edit.php?id='.$data['id_info'].'">Edit</a> / <a href="delete.php?id='.$data['id_info'].'" onclick="return confirm(\'Are You Sure?\')">Delete</a></td>';
echo '</tr>';
$no++;
}
}
?>
答案 0 :(得分:2)
假设正确使用索引键,您可以使用INNER JOIN:
SELECT country.country_name,
state.state_name,
city.city_name
FROM information
INNER JOIN country ON information.country_id = country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
答案 1 :(得分:1)
尝试更改GROUP BY并使用INNER JOIN:
SELECT *
FROM information
INNER JOIN country ON information.country_id =country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
GROUP BY information.country_id, information.state_id, information.city_id
答案 2 :(得分:0)
现在有效,*叹息问题是信息表,我必须先清空表。
我发生了很多事,记录是问题来源,我已经经历了很多这样的事情。谢谢大家的帮助,现在它的工作非常好。
上面的两个答案完美无缺,谢谢。