在WHERE子句中选择语句

时间:2014-08-08 14:53:52

标签: sql select nested

我在尝试理解如何从我的sql数据库中获取一些数据时遇到了问题。

假设我有一个数据库

name    worktype    completed 
dave    word        2014-01-01
john    excel       2014-02-07
dave    excel       2014-04-04
dave    access      2014-05-18
dave    word        0000-00-00
john    word        2014-08-25
dave    word        2014-08-13
sue     word        2014-01-10
dave    word        2014-07-21
dave    word        0000-00-00

现在我想从这个数据库中获取; (所有工作类型> ='2014-07-01')+(工作类型为“word”并且已完成日期“0000-00-00”)

SELECT worktype FROM workdb1 WHERE worktype = 'word' and completed = '0000-00-00' UNION
SELECT worktype FROM workdb1 WHERE completed >= '2014-07-01;

并将结果放入第二个数据库

INSERT INTO workdb2 VALUES(SELECT worktype FROM workdb1 WHERE completed >= '2014-07-01' AND worktype = 'word' AND worktype = ALL AND completed = '0000-00-00'

这基本上就是我想要的,但那不起作用。

这是子查询吗?加入查询?

抱歉,按“输入”并在我完成问题之前提交

由于

1 个答案:

答案 0 :(得分:5)

您可以使用or来解决这两个问题:

SELECT worktype
FROM (database)
WHERE (completed >= '2014-07-01') or
      (worktype = 'word' and completed = '0000-00-00');