选择列没有返回现有结果的所有行。

时间:2022-09-27 07:57:05

I am trying to pull records up from a mysql database but I want the result in one column to be completely unique

我试图从mysql数据库中提取记录,但我希望在一个列中结果是完全惟一的。

below is an example table

下面是一个示例表。

fname  | lname  | Street
-----------------------
john   | brown  | camelot st
george | kent   | camelot st
fred   | kent   | johnston rd
jane   |williams| camelot st
jack   | johnson| archer dr
james  | smith  | bruce st
adam   | smith  | james dr

below is the results I would want

下面是我想要的结果。

Street
-----------
camelot st
johnston rd
archer dr
bruce st
james dr

in other words the first time any street name is returned it should display in the results after the first time it is no longer unique in the result and it should be omitted.

换句话说,第一次返回任何街道名称时,它应该显示在第一次之后的结果中,它不再是唯一的结果,应该被省略。

How would I manage this in mysql

mysql中如何管理?

1 个解决方案

#1


2  

SELECT * FROM tableName 
WHERE street IN (
    SELECT DISTINCT street FROM tableName
) LIMIT 1;

This query should answer your question. However, it is important to note that you should order by an id field, because otherwise there is no guarantee that the rows in your database will always be queried in the same order.

这个查询应该能回答你的问题。但是,需要注意的是,您应该通过id字段进行排序,否则无法保证数据库中的行总是以相同的顺序被查询。

#1


2  

SELECT * FROM tableName 
WHERE street IN (
    SELECT DISTINCT street FROM tableName
) LIMIT 1;

This query should answer your question. However, it is important to note that you should order by an id field, because otherwise there is no guarantee that the rows in your database will always be queried in the same order.

这个查询应该能回答你的问题。但是,需要注意的是,您应该通过id字段进行排序,否则无法保证数据库中的行总是以相同的顺序被查询。