SQL - 如果id的所有bool值都为true,则更新

时间:2017-05-19 20:43:15

标签: mysql sql

我有三张桌子:

任务:(t_id是主键,all_done是布尔值)

+------+-------------+
| t_id |    all_done |
+------+-------------+
|    1 |           0 |
|    2 |           0 |
+------+-------------+

子任务:( s_id是主键,完成是布尔值)

+------+------------+
| s_id |       done |
+------+------------+
|    1 |          0 |
|    2 |          0 |
|    3 |          1 |
|    4 |          1 |
|    5 |          1 |
|    6 |          0 |
|    7 |          0 |
+------+------------+

task_subtask :( s_id和t_id是外键。并非每个子任务都属于任务)

+------+------+
| t_id | s_id |
+------+------+
|    1 |    1 |
|    1 |    2 |
|    1 |    3 |
|    2 |    4 |
|    2 |    5 |
+------+------+

task_subtask 表示哪个子任务属于哪个任务 我的目的是进行sql调用以设置 all_done = 1 ,如果该任务的所有子任务都已完成。因此,在此示例中,对于 t_id = 2

all_done 应设置为1

以下代码似乎有效,但我无法将其填写为UPDATE电话,而且我不确定我是否正在考虑正确的方向:

SELECT x.t_id, count(x.t_id), SUM(x.done) FROM (
    SELECT subtask.s_id, subtask.done, task_subtask.t_id FROM subtask JOIN task_subtask WHERE subtask.s_id = task_subtask.s_id
) AS x GROUP BY x.t_id;

通过该声明,我得到了这个输出:

+------+---------------+-------------+
| t_id | count(x.t_id) | SUM(x.done) |
+------+---------------+-------------+
|    1 |             3 |           1 |
|    2 |             2 |           2 |
+------+---------------+-------------+

我最好的尝试可能就是这样:

UPDATE task, (SELECT x.t_id, count(x.t_id), SUM(x.done) FROM (
        SELECT subtask.s_id, subtask.done, task_subtask.t_id FROM subtask JOIN task_subtask WHERE subtask.s_id = task_subtask.s_id
    ) AS x GROUP BY x.t_id)
SET task.all_done = 1 WHERE task.t_id = t_id AND count(x.t_id) = SUM(x.done);

此次调用的错误消息是

  

错误1248(42000):每个派生表必须有自己的别名

但我不确定更新通话是否会以这种方式运作。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您使查询变得复杂。我认为这应该有用。

{{1}}

答案 1 :(得分:0)

如果您只是将task_subtask合并为同一个表,则添加T_ID字段

task_table:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let aurl = NSURL(string: "http://georgetown.neotalogic.com/a/jobdefender")
    let request = NSURLRequest(url: aurl! as URL)

    Webview.loadRequest(request as URLRequest)
    Webview.scalesPageToFit = true

subtask_table :( s_id,t_id是主键)

+------+-------------+
| t_id |    all_done |
+------+-------------+
|    1 |           0 |
|    2 |           0 |
+------+-------------+

现在你可以试试

+------+------+------------+
| s_id | t_id |       done |
+------+-------------------+
|    1 |    1 |          0 |
|    2 |    1 |          0 |
|    3 |    1 |          1 |
|    4 |    2 |          1 |
|    5 |    2 |          1 |
 etc...
+------+------|------------+

这意味着,对于每个任务,如果没有更多的子任务,其中done = 0,则更新任务集all_done = 1

另一个好方法:在更新subTask之后创建一个触发器,使用相同的逻辑。

UPDATE TASK SET all_done=1 WHERE 
count
    (Select done from subtask join task 
    on subtask.t_id=task.t_id WHERE done=0)=0

我现在看一下第三个解决方案,只使用一个表:

CREATE TRIGGER set_all_done
AFTER UPDATE
   ON subtask FOR EACH ROW

BEGIN    
   IF count(Select done from subtask  WHERE subtask.t_id = new.t_id)=0
then UPDATE TASK SET all_done=1 WHERE Task.t_id = new.t_id

END;

此单行返回t_id尚未完成,您可以将其用作验证,或

SELECT distinct t_id from subtask where done=0