简体   繁体   English

选择*不同的一列

[英]Select * distinct one column

I have mysql table tmp with columns pid,city,state,country. 我有mysql表tmp,其列为pid,city,state,country。 I write queries so i can find matching city,state or country, and pid is field that helps me load another table. 我写查询,以便我可以找到匹配的城市,州或国家,并且pid是可以帮助我加载另一个表的字段。 The thing is, there is always two rows with same pid, and sometimes (when WHERE find matching city state or country in both), i display data from additional table twice unnecessarily. 问题是,总是有两行具有相同的pid,有时(当在其中找到匹配的城市州或国家/地区时),我不必要地两次显示来自附加表的数据。 So i need to select something like: 所以我需要选择类似的东西:

SELECT * FROM tmp DISTINCT pid WHERE city='test'

I have no idea how to search solution (i searched here on stackoverflow, but no luck). 我不知道如何搜索解决方案(我在这里搜索stackoverflow,但没有运气)。 Also, there will be a lot of searching in this table, so if there is multiple solutions i would prefer one that is faster. 另外,此表中将进行大量搜索,因此,如果有多个解决方案,我希望使用速度更快的解决方案。 Thanks 谢谢

请尝试以下SQL语句:

SELECT DISTINCT pid FROM tmp WHERE city='test'

尝试这个

SELECT DISTINCT pid,field1,field2 FROM tmp WHERE city='test'
$keyword="0";
$query = "SELECT DISTINCT titel,id FROM xyz ORDER BY titel ASC ";
$result = mysql_query($query); 
while ($row = mysql_fetch_array($result)) 
{ 
    if($keyword!==$row["titel"])
    {
        $keyword=$row["titel"];
        echo $keyword;
    }
}

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

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