简体   繁体   English

如何在同一列中选择多个值?

[英]How do I select multiple values in the same column?

I am trying to select multiple values in a single column. 我试图在一列中选择多个值。 Basically I want the query to select all those under column family with values Software_1Y , XI_1Y and P1_1Y 基本上我希望查询选择具有值Software_1YXI_1YP1_1Yfamily下的所有那些

I am running this query : 我正在运行此查询:

SELECT `salesorder`
    ,`masterproduct`
    ,`family`
    ,`birthstamp`
    ,`duedate`
    ,COUNT(*) AS `total`
FROM `report`
WHERE `birthstamp` BETWEEN '$startDT'
        AND '$endDT'
    AND `family` = 'Software_1Y'
    AND `family = 'XI_1Y'
    AND `family` = 'PI_1Y'
GROUP BY `salesorder`
    ,`masterproduct`
    ,`family`
    ,`duedate`;

My query returns no rows but is I search each family one by one, I have values. 我的查询没有返回任何行但是我逐个搜索每个家庭,我有值。

What is wrong with my query? 我的查询有什么问题?

Also, my purpose is to get all the rows whose family values are Software_1Y , XI_1Y and PI_1Y . 另外,我的目的是获取其family值为Software_1YXI_1YPI_1Y所有行。

How about using IN instead 如何使用IN代替

SELECT  `salesorder`,
        `masterproduct`,
        `family`,
        `birthstamp`,
        `duedate`, 
        COUNT( * ) AS `total` 
FROM    `report` 
WHERE   `birthstamp` BETWEEN '$startDT' AND '$endDT' 
AND     `family` IN ('Software_1Y','XI_1Y','PI_1Y')
GROUP BY    `salesorder`,
            `masterproduct`,
            `family`,
            `duedate`;

The reason why no values are returned, is because of this section 没有返回值的原因是因为本节

AND `family` = 'Software_1Y' 
AND `family = 'XI_1Y' 
AND `family` = 'PI_1Y'

family cannot be all 3 values at once, but it might be 1 of the 3 values. family不能同时为3个值,但可能是3个值中的1个。

That is why you would use IN. 这就是你使用IN的原因。

Another way to look at it would be to use OR, but that gets really long winded. 另一种看待它的方法是使用OR,但这会很长。

$query="   SELECT `salesorder`,`masterproduct`,`family`,`birthstamp`,`duedate`, COUNT( * ) AS `total` FROM `report` 
    WHERE `birthstamp` BETWEEN '$startDT' AND '$endDT' 
           AND (`family` = 'Software_1Y' 
           OR `family` = 'XI_1Y' 
           OR `family` = 'PI_1Y') 
    GROUP BY `salesorder`,`masterproduct`,`family`,`duedate` ";

It must be due to AND instead of OR while querying FAMILY column. 在查询FAMILY列时,必须使用AND而不是OR。

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) 
{
//Operation you want to perform
}

@jude: It seems you are directly passing the query which is of type string directly as a parameter to mysql_fetch_array, which is wrong. @jude:看来你直接将类型为string的查询作为参数直接传递给mysql_fetch_array,这是错误的。 Instead follow the above approach... 而是按照上述方法......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM