Php 基于另一列的值获取一列的值

Php 基于另一列的值获取一列的值,php,mysql,sql,Php,Mysql,Sql,我有这张桌子 TableTBL: +--------+------+-------+ | ID | Col1 | Col2 | +--------+------+-------+ | 1 | A | 30 | | 2 | A | 20 | | 3 | B | 40 | | 4 | A | 10 | +--------+------+-------+ 现在我想从Col1得到Col2的值

我有这张桌子

TableTBL: 
+--------+------+-------+
|    ID  | Col1 | Col2  |
+--------+------+-------+
|      1 |    A |    30 | 
|      2 |    A |    20 | 
|      3 |    B |    40 |  
|      4 |    A |    10 |  
+--------+------+-------+
现在我想从Col1得到Col2的值,假设我想要A的所有值,结果是[30,20,10]

我尝试了以下查询,但似乎效果不佳:

SELECT DISTINCT Col2 FROM TableTBL WHERE Col1 = 'A' ORDER BY Col2 DESC;
我在php中的foreach循环中使用了它,下面是带有php代码的查询:

$sql = "SELECT DISTINCT Col1 FROM TableTBL ORDER BY Col1 DESC;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql )) 
{
    echo "SQL statement failed";
}
else
{
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    while ($row = mysqli_fetch_assoc($result)) 
    { 
        $rows[] = $row['Col1']; 

    } 
    for($i = 0; $i < count($rows) ;$i++)
    {
        echo $rows[$i].": {\n";

        $sql = "SELECT DISTINCT Col2 FROM TableTBL WHERE Col1 = '$rows[$i]' ORDER BY Col2 DESC;";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) 
        {
            echo "SQL statement failed";
        }
        else
        {
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);

            while ($row = mysqli_fetch_assoc($result)) { 
                $rows[] = $row; 
            } 
            foreach($rows as $row) {
                $arrList[] = $row['Col2'];
            }
            $text =  implode("',' ", array_unique($arrList));
        }
    }
}
$sql=“按Col1 DESC;从表中选择不同的Col1”;
$stmt=mysqli\u stmt\u init($conn);
如果(!mysqli_stmt_prepare($stmt,$sql))
{
echo“SQL语句失败”;
}
其他的
{
mysqli_stmt_execute($stmt);
$result=mysqli\u stmt\u get\u result($stmt);
while($row=mysqli\u fetch\u assoc($result))
{ 
$rows[]=$row['Col1'];
} 
对于($i=0;$i
此处
$text
是包含结果的字符串,结果用逗号分隔


我在这里遇到的问题是,除了第一次迭代之外,我在所有迭代中都得到了重复的结果,它给了我一个30,20,40,10的A,但它给了我一个30,20,40,10的B。

正如@Barmar所建议的,使用一个查询:

$sql = "SELECT Col1, 
               GROUP_CONCAT(DISTINCT Col2 ORDER BY Col2 DESC SEPARATOR ', ') AS Col2 
        FROM TableTBL 
        GROUP BY Col1
        ORDER BY Col1 ";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL statement failed";
} else {
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $arrList = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $arrList[] = array(
            'Col1' => $row['Col1'],
            'Col2' => $row['Col2']
        );
    }
    var_export($arrList);
}
输出

array(
    0 => array(
        'Col1' => 'A',
        'Col2' => '30, 20, 10',
    ),
    1 => array(
        'Col1' => 'B',
        'Col2' => '40',
    ),
)
检查我的电脑


如果您不知道
GROUP_CONCAT()
他是一个

查询很好:为什么你认为它不好用?好的,很好,但问题是什么?你不需要两个循环,你可以在
循环中分配给
$arrList
。@AlbertoMoro非常抱歉,问题不完整,编辑了详细的问题。@Barmar我编辑了详细的问题。@Barmar问题,对不起