我遇到了MySQL查询或我的逻辑问题。
我需要使用我的DATABASE中的值创建一个html表。但是在我的表的某些字段中可以是多个值。我该怎么做我的桌子?我可以在一个查询中执行此操作,还是应该在表完成后执行第二次查询?
以下是表格:
offers:
id | rid | name
------------------------------
1 | 1234 | mary
2 | 1235 | john
3 | 5342 | liam
geo_in_off:
offer_id | geo_id
------------------------------
1 | 1
1 | 2
3 | 3
有些优惠可以包含2个地理位置。所以我需要一个看起来像的htm ltable:
No | rid | name | geo
----------------------------------
1 | 1234 | mary | 1, 2
但是我得到了一张与公众相似的表:
No | rid | name | geo
----------------------------------
1 | 1234 | mary | 1
2 | 1234 | mary | 2
我该如何解决?
这是我的代码:
require('../config.php');
$sql ="SELECT * FROM `offers` LEFT JOIN `geo_in_off` ON `geo_in_off`.`offer_id` = `offers`.`id` ORDER BY `offers`.`rid`";
$result = mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_assoc($result)) {
echo "<tr><td>" . $i . "</td><td class=\"editable\">" . $row['rid'] . "</td><td class=\"editable\">" . $row['name'] . "</td><td>" .$row['geo_id'] . "</td></tr>" ;
$i++;
}
这是config.php:
$db_name = "dbname";
$db_user = "user";
$db_pass = "pass";
$db_connect = mysql_connect('localhost', $db_user, $db_pass);
$selected = mysql_select_db($db_name, $db_connect);
@mysql_query('set character_set_results="utf8"');
@mysql_query('set collation_connection="utf8_general_ci"');
@mysql_query('set character_set_client="utf8"');
答案 0 :(得分:0)
1st:使用group_concat
和group by
子句
GROUP_CONCAT()
将返回一个字符串,其中non-NULL
group
值为SELECT offers.rid,offers.name,group_concat(geo_in_off.geo_id)
FROM `offers`
LEFT JOIN `geo_in_off`
ON `geo_in_off`.`offer_id` = `offers`.`id`
GROUP BY `offers`.`rid`,offers.name
ORDER BY `offers`.`rid`
GROUP_CONCAT
注意: comma(,)
函数默认分隔符为custom
。也可以使用mysql_*
一个。
第二名: deprecated
mysqli_*
尝试使用pdo or prepared statement
第3名:还尝试使用require('../config.php');
$sql ="SELECT `offers`.`rid` as Rid ,`offers`.`name` as Name ,group_concat(geo_in_off.geo) as geos
FROM `offers`
LEFT JOIN `geo_in_off`
ON `geo_in_off`.`offer_id` = `offers`.`id`
GROUP BY `offers`.`rid`,`offers`.`name`
ORDER BY `offers`.`rid`";
$stmt = $conn->prepare($sql);
$stmt->execute();
$get_result =$stmt->get_result();
$row_count= $get_result->num_rows;
if($row_count>0){
echo "<table><thead><tr><th>No</th><th>Rid</th><th>name</th><th>goes</th></tr></thead><tbody>";
$i=0;
while ($row=$get_result->fetch_assoc()) {
echo "<tr><td>" . $i . "</td><td class=\"editable\">" . $row['Rid'] . "</td><td class=\"editable\">" . $row['Name'] . "</td><td>" .$row['geos'] . "</td></tr>";
$i++;
}
echo "</tbody></table>";
}
PHP:
$db_name = "dbname";
$db_user = "user";
$db_pass = "pass";
$conn = mysqli_connect('localhost', $db_user, $db_pass,$db_name) or die('error'.mysqli_connect_error());
mysqli_query($conn,'set character_set_results="utf8"');
mysqli_query($conn,'set collation_connection="utf8_general_ci"');
mysqli_query($conn,'set character_set_client="utf8"');
<强> CONFIG.PHP 强>
this
答案 1 :(得分:0)
您应该使用单独的表来存储“geo”的值。多值属性会导致数据库中的数据冗余以及数据库范例形式的数据冗余。
即
No | rid | name |
----------------------------------
1 | 1234 | mary |
2 | 1234 | mary |
No | geo | foreigh_key |
----------------------------------
1 | 1 | 1 |
2 | 2 | 1 |