sql query selecting one name no matter how many rows it was mentioned in
-
15-06-2021 - |
Question
Basically what I'm trying to do is get the information from column x no matter how many times it was mentioned. means that if I have this kind of table:
x | y | z
------+-------+--------
hello | one | bye
hello | two | goodbye
hi | three | see you
so what I'm trying to do is create a query that would get all of the names that are mentions in the x column without duplicates and put it into a select list.
my goal is that I would have a select list with TWO not THREE options, hello and hi
this is what I have so far which isn't working. hope you guys know the answer to that:
function getList(){
$options="<select id='names' style='margin-right:40px;'>";
$c_id = $_SESSION['id'];
$sql="SELECT * FROM names";
$result=mysql_query($sql);
$options.="<option value='blank'>-- Select something --</option>" ;
while ($row=mysql_fetch_array($result)) {
$name=$row["x"];
$options.="<option value='$name'>$name</option>";
}
$options.= "</SELECT>";
return "$options";
}
Sorry for confusing... i edited my source
Solution
You seem to be using only x
. So you can just use query:
SELECT DISTINCT x FROM names
OTHER TIPS
if this is your query
$sql="SELECT * FROM names";
$result=mysql_query($sql);
Then
$sql="SELECT DISTINCT x FROM names";
$result=mysql_query($sql);
Change * to x if you want only one column as it is faster
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow