PHP:MY​​SQL查询中有多个AND条件?

时间:2015-11-15 00:03:53

标签: php mysql

我正在试图找出为什么我无法在mysql查询中运行多个AND条件。

基本上这是我的代码:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' AND category_name='Boys Clothes' GROUP BY category_name

上面的代码没有返回任何内容,但是当我像这样单独运行代码时:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' GROUP BY category_name

或者像这样:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Boys Clothes' GROUP BY category_name

然后它运作正常!

这是一个基本问题,遗憾的是我无法弄清楚如何解决它。

有人可以就此问题提出建议吗?

3 个答案:

答案 0 :(得分:2)

这是因为查询的逻辑是关闭的。如果你考虑一下,你就不能有一个名字叫做Girls Clothes And Boys Clothes的类别名称。但是,您可以得到名称为Girls Clothes OR Boys Clothes的结果。

查询应如下所示:

using System.Numerics;                      // needed for BigInteger
    /* Methods ************************************************************/
    private static BigInteger sfactor(BigInteger k) // factor odd integers
    {
        BigInteger x, y;
        int flag;
        x = y = iSqrt(k);                   // Integer Square Root
        if (x % 2 == 0) { x -= 1; y += 1; } // if even make x & y odd
        do
        { 
            flag = BigInteger.Compare((x*y), k);
            if (flag > 0) x -= 2;
            y += 2;
        } while(flag != 0);
        return x;                           // return x
    } // end of sfactor()

    // Finds the integer square root of a positive number
    private static BigInteger iSqrt(BigInteger num)
    {
        if (0 == num) { return 0; }           // Avoid zero divide
        BigInteger n = (num / 2) + 1;         // Initial estimate, never low
        BigInteger n1 = (n + (num / n)) >> 1; // right shift to divide by 2
        while (n1 < n)
        {
            n = n1;
            n1 = (n + (num / n)) >> 1;        // right shift to divide by 2
        }
        return n;
    } // end iSqrt()

答案 1 :(得分:2)

我猜你想要两个类别的网址。如果是这样的话:

SELECT url
FROM mytable 
WHERE category_name IN ('Girls Clothes', 'Boys Clothes')
GROUP BY url
HAVING COUNT(*) = 2;

如果您只想检查任一类别,请在查询中使用IN(或OR)。

答案 2 :(得分:1)

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' AND category_name='Boys Clothes' GROUP BY category_name

因为一件商品不可能有2 category_name s。

你可能想要的是找到它,如果它是&#34;女孩衣服&#34;或&#34;男孩衣服&#34;。在这种情况下,您将使用OR代替AND。

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' OR category_name='Boys Clothes' GROUP BY category_name