目标:
从数据库中选择以从我的五个特定列之一定位团队名称。该表格包含多个列,其中5列为respondent_team_1
到respondent_team_5
。在每个列中都可能有一个重复的字符串值,所以想法是查看那些列并知道这一点并且只取1个不同的值而不是全部,所以我可以像这样匹配数据:
CODE:
<?php
$sql = "SELECT distinct respondent_team_1 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_1"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_2 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_2"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_3 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_3"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_4 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_4"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_5 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_5"];
}
}
?>
概要
我想要做的是将所有这些代码块细化为一个代码块,因为它很乱,B必须有更好的方法,但我找不到一个?
我尝试了各种方法,显而易见的是采用我需要的列名并对它们进行选择,我尝试使用GROUP BY关键字也没有运气。我不是在寻找有人为我这样做,但只是一些智慧或建议会非常好。