繁体   English   中英

与一张桌子不同,另一张桌子价值

[英]Distinct from one table, value from another

我不确定提出此问题的正确方法:我想从一个表中选择一个不同的值,然后从另一个表中获取该值。

$sql1 = mysql_query("SELECT DISTINCT County_ID FROM Customers ORDER BY County_ID ASC") or die(mysql_error());
while($row1 = mysql_fetch_array( $sql1 ))
{
    $sql2 = mysql_query("SELECT County_Value FROM Counties") or die(mysql_error());
    while($row2 = mysql_fetch_array( $sql2 ))
        {
            echo "Listed Counties : ".$row2['County_Value']." - id : ".$row1['County_ID']."<br>";
        }
}

但是通过这种方式我得到了ORDER BY县ID,正确的方法是什么,以及ASC中的值列表? 谢谢。

$sql1 = mysql_query("SELECT DISTINCT County_ID, County_Value FROM Customers, Counties WHERE Counties.County_ID=Customer.County_ID ORDER BY County_Value ASC") or die(mysql_error());

PS:我应该说我想从客户表中获取列出的县列表,但是它们以id的形式存储在县表中,因此我只需要不同的countie_values ASC

也许是这样的:

SELECT CustomersSub.County_ID, County_Value 
FROM (SELECT DISTINCT County_ID FROM Customers) CustomersSub
INNER JOIN Counties 
ON Counties.County_ID = CustomersSub.County_ID 
ORDER BY County_Value ASC

编辑-或使用汇总功能并计算每个县的客户:-

SELECT Counties.County_ID, County_Value, COUNT(Customers.County_ID) AS CustCountyCount
FROM Counties 
INNER JOIN Customers
ON Counties.County_ID = Customers.County_ID 
GROUP BY Counties.County_ID, County_Value
ORDER BY County_Value ASC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM