获取/检索另一列的同一值下的多个字段值中的一个字段值


Get/retrieve one field value of many under the same value of another column

我不知道如何表达这个问题,也不知道如何正确查找。我试过很多措辞不同的搜索,但还没有找到任何结果。我是mysql和php的新手,所以很抱歉有任何noobish的评论。

+---+-----------+---------+
|state  |County |people   |
+---+-----------+---------+
|state1 | a     | person1 |
|state1 | a     | person2 |
|state1 | b     | person3 |
|state2 | c     | person4 |
|state2 | d     | person5 |
+---+-----------+---------+

这是我正在处理的表格的一个非常简短的例子。我正在寻找一种方法来获得每个州的第一个县。这是一个网页,显示了所有不同的人指导学生的位置,因此还有很多内容。

Choose State
-State1  -State2
Counties in State1:
 -a      -b
County a in State1:
-Person1 
-Person2

我想要它,这样当你点击state1的href链接时,它将以县a作为默认值来显示该州的哪些人。否则,它将显示最后一次选择或默认为其他州的县。然后,您可以单击该州显示的任何县的href链接。

我一直在想,我可以在表格中搜索州1,然后得到第一个县,但我不知道该怎么做。我一直在想我能做到:

($row['State'])['County']

但对于php 来说,这似乎真的是非法的

你对这个东西的使用似乎完全无关(尽管很好),但为了回答你的字面问题,你可以使用LIMIT:

select county         -- we want the county name
from table            -- your table name here
where state='state1'  -- your search criteria
order by county       -- alphabetical ordering to select the first county (if needed)
limit 1               -- and we only care about the first!