这是我数据库中的表格
+--------------------+
| Tables_in_db |
+--------------------+
| it_survey |
| lvis_survey |
| schools |
| subjects |
| supplies_survey |
| users |
+--------------------+
以下是我根据用户对这三个表中每个表的ID号发布的调查总数的查询:
select count(user_id) AS posted_surveys from
(select user_id from it_survey where user_id = 7 UNION ALL
select user_id from lvis_survey where user_id = 7 UNION ALL
select user_id from supplies_survey where user_id = 7) tables;
上面的查询将按以下方式返回值:
+--------------------+
| posted_surveys |
+--------------------+
| 2 |
+--------------------+
现在我想要实现的是基本上颠倒这个查询并获得基于的未发布调查的价值 调查表的数量。我想我必须从posting_surveys计算中减去表的数量,但不知道如何实现这一点。
这甚至可能吗?
答案 0 :(得分:0)
将IF
与EXISTS
和子查询一起使用:
SELECT IF(EXISTS (SELECT * FROM it_survey WHERE user_id = 7), 0, 1)
+ IF(EXISTS (SELECT * FROM lvis_survey WHERE user_id = 7), 0, 1)
+ IF(EXISTS (SELECT * FROM supplies_survey WHERE user_id = 7), 0, 1) AS unposted_surveys
反转0, 1
参数的顺序以获取已过帐调查的计数。