Im trying to select distinct names from 'Specifically' but only where col Type = a specific value.
For example I have
rowID Type Specifically
1 cat Ocicat
2 cat Bombay
3 cat Bombay
4 cat Bengal
5 cat Savannah
6 dog Collie
7 dog Keeshond
8 dog Pug
9 dog Keeshond
10 dog Pug
11 dog Doberman
12 Horse n/a
And I want to read out something like
type=cat and specific=Bengal
type=cat and specific=Bombay
type=cat and specific=Ocicat
type=cat and specific=Savannah
I currently have this but its giving me 'Undefined index: Type'
$Result = mysql_query("select distinct Specifically from pet_table WHERE Type='cat' ORDER BY Specifically asc ");
while($row = mysql_fetch_array($Result))
{
PRINT("type=".$row['Type']." and specific=".$row['Specifically']."<BR>");
}
I would also like to do something like where Type is not cat OR dog OR horse... ect
Thanks
You want GROUP BY
SELECT Type, Specifically FROM pet_table
WHERE Type='cat'
GROUP BY Specifically
ORDER BY Specifically asc
The index doesn't exist because you're not selecting it. Include Type
in your SELECT
statement to fix it.
You get the Undefined Index error, because you are not fetching the Type
column, but are trying to use it.
Change your query to.
select type, distinct Specifically from pet_table
WHERE Type='cat' ORDER BY Specifically asc
Secondly, if you want Type not cat or dog or horse, you can use the NOT IN
clause.
Finally, please don't use MySQL as it is not maintained and is deprecated. Consider using, MySQLi, or PDO.
The reason you're getting this error is because you're not selecting Type
.
$res = mysql_query("SELECT DISTINCT `Specifically`,`Type` from `pet_table` WHERE `Type`='cat' ORDER BY `Specifically` ASC");
while($row = mysql_fetch_assoc($res))
{
echo "The type is {$row['Type']} and it is specifically a {$row['Specifically']}
}