php - mysql SELECT distinct row from col1 WHERE col2=x

标签 php mysql distinct

我试图从“Specifically”中选择不同的名称,但仅限于 col Type = 特定值的情况。

例如我有

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

我想读出类似的东西

type=cat and specific=Bengal
type=cat and specific=Bombay
type=cat and specific=Ocicat
type=cat and specific=Savannah

我目前有这个,但它给了我“ undefined index :类型”

$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>");
}

我还想做一些类似类型不是猫或狗或马的事情......等等

谢谢

最佳答案

索引不存在,因为您没有选择它。在您的 SELECT 语句中包含 Type 以修复它。

关于php - mysql SELECT distinct row from col1 WHERE col2=x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368483/

相关文章:

php - 为玩家数量可变的多人游戏设计 MySQL 表

c# - 如何使用 DISTINCT 在 NHibernate SQL 查询中进行分页

mysql - sql group by 与 distinct

c# - 为什么此 Entity Framework LINQ 查询不会产生 SELECT DISTINCT?

php - 可调整大小的表格区域 PHP

php - 如何在 Twig 中定义数组

PHP mail() 函数不会发送到 gmail 但会发送到我的非 gmail 帐户

php - 以表号形式给出数据库中的数据

php - 尝试向 MySQL 插入数据时出错

mysql - 如何转换日期并更新连接表