在sql查询中没有AND运算符的结果

时间:2017-10-24 09:36:09

标签: mysql

如果我在查询中使用和运算符,为什么我没有得到答案?

我的表

+-----------+--------+----------+------+----------+-------------+--------------+
| course_id | sec_id | semester | year | building | room_number | time_slot_id |
| BIO-301   | 1      | Summer   | 2010 | Painter  | 514         | A            |
| CS-101    | 1      | Fall     | 2009 | Packard  | 101         | H            |
| CS-101    | 1      | Spring   | 2010 | Packard  | 101         | F            |
| CS-190    | 1      | Spring   | 2009 | Taylor   | 3128        | E            |
+-----------+--------+----------+------+----------+-------------+--------------+

SQL QUERY WITH OUTPUT

MariaDB [university2038]> 

    select course_id from section where semester='Fall' AND semester='Spring';

Empty set (0.00 sec)

3 个答案:

答案 0 :(得分:2)

WHERE semester='Fall' AND semester='Spring'将始终不会产生任何行,因为单元格不能是值' Fall'和价值'春天'同时。您正在寻找OR条件。

答案 1 :(得分:2)

  

semester ='Fall'AND semester ='Spring'

同时semester的“秋天”和“春天”的价值怎么样?

这是一种常见的错误,它出现在口语的邋。之中。我们说“我希望在下一个春季和秋季安排课程。”

但是这个陈述背后的逻辑(布尔)条件是“在春天期间

可以在SQL中将其表达为:

SELECT course_id FROM section WHERE semester = 'Fall' OR semester = 'Spring'

使用IN operator实现更简单(也更容易理解)的方式来编写相同的内容:

SELECT course_id FROM section WHERE semester IN ('Fall', 'Spring')

答案 2 :(得分:0)

你实际上没有说过你想要的外观,所以以下任何一个都是合适的。

MariaDB [sandbox]> drop table if exists p;
Query OK, 0 rows affected (0.13 sec)

MariaDB [sandbox]> create table p
    -> ( course_id VARCHAR(10), sec_id INT, semester VARCHAR(100), year INT, building VARCHAR(10), room_number INT, time_slot_id varchar(1));
Query OK, 0 rows affected (0.27 sec)

MariaDB [sandbox]>  insert into p values
    -> ( 'BIO-301'   , 1      , 'Summer'   , 2010 , 'Painter'  , 514         , 'A'),
    -> ( 'CS-101'    , 1      , 'Fall'     , 2009 , 'Packard'  , 101         , 'H'),
    -> ( 'CS-101'    , 1      , 'Spring'   , 2010 , 'Packard'  , 101         , 'F'),
    -> ( 'CS-190'    , 1      , 'Spring'   , 2009 , 'Taylor'   , 3128        , 'E');
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> SELECT COURSE_ID
    -> FROM   P
    -> WHERE SEMESTER IN ('Fall','Spring')
    -> GROUP BY COURSE_ID HAVING COUNT(*) > 1;
+-----------+
| COURSE_ID |
+-----------+
| CS-101    |
+-----------+
1 row in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select *
    -> from p
    -> where course_id in
    -> (SELECT COURSE_ID
    -> FROM   P
    -> WHERE SEMESTER IN ('Fall','Spring')
    -> GROUP BY COURSE_ID HAVING COUNT(*) > 1
    -> )
    -> ;
+-----------+--------+----------+------+----------+-------------+--------------+
| course_id | sec_id | semester | year | building | room_number | time_slot_id |
+-----------+--------+----------+------+----------+-------------+--------------+
| CS-101    |      1 | Fall     | 2009 | Packard  |         101 | H            |
| CS-101    |      1 | Spring   | 2010 | Packard  |         101 | F            |
+-----------+--------+----------+------+----------+-------------+--------------+
2 rows in set (0.00 sec)

MariaDB [sandbox]> select p.*
    -> from p
    -> join
    -> (select course_id,
    -> sum(case when semester in('Spring','Fall') then 1 else 0 end) as cnt
    -> from p
    -> group by course_id
    -> ) s
    -> on s.course_id = p.course_id
    -> where s.cnt = 2
    -> ;
+-----------+--------+----------+------+----------+-------------+--------------+
| course_id | sec_id | semester | year | building | room_number | time_slot_id |
+-----------+--------+----------+------+----------+-------------+--------------+
| CS-101    |      1 | Fall     | 2009 | Packard  |         101 | H            |
| CS-101    |      1 | Spring   | 2010 | Packard  |         101 | F            |
+-----------+--------+----------+------+----------+-------------+--------------+
2 rows in set (0.00 sec)