SQL Select Statement with WHERE, AND, OR

前端 未结 5 700
春和景丽
春和景丽 2021-02-05 19:05

I would like to perform a SELECT query with MySQL. My goal is to select all the dogs in a vet database that would be sex=male and fur=short

相关标签:
5条回答
  • 2021-02-05 19:39

    Make sure to add parentheses, so the OR condition gets evaluated correctly.

    SELECT name, sex, fur, color 
    FROM dogs 
    WHERE sex='male' AND fur='short' AND (color='black' OR size='big');
    
    0 讨论(0)
  • 2021-02-05 19:42

    I have used this and its working;

    SELECT name, sex, fur, color 
    FROM dogs 
    WHERE sex="male" AND fur="short" AND (color="black" || size="big");
    
    0 讨论(0)
  • 2021-02-05 19:45

    According to Operator precedence for MySQL AND has higher precedence than OR.

    So C1 AND C2 OR C3 will be treated as (C1 AND C2) OR C3

    To override the default precedence you need to use parenthesis as:C1 AND (C2 OR C3)

    In your case the right query is:

    SELECT name, sex, fur, color 
    FROM dogs 
    WHERE sex='male' AND fur='short' AND (color='black' OR size="big");
    
    0 讨论(0)
  • 2021-02-05 19:56

    The way you use parentheses in the description of your goal is correct. The syntax is:

    SELECT name, sex, fur, color 
        FROM dogs 
        WHERE sex="male" AND fur="short" AND (color="black" OR size="big");
    
    0 讨论(0)
  • 2021-02-05 19:56

    select name, sex, fur,color from dogs where sex ='male' and fur = 'short' and (color ='black' or size ='big');

    0 讨论(0)
提交回复
热议问题