简体   繁体   中英

Select * distinct one column

I have mysql table tmp with columns 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. 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. 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). 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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