是否可以创建这样的MySQL查询?

时间:2019-02-24 15:50:44

标签: php mysql sql php-mysqlidb

我有两个桌子。我想创建一个查询,该查询返回一个表中的所有行,其中表中列的整数值之和小于特定值。

想像一下表是这样的:

template <typename T> class C { public: // Common code... void only_for_bool() requires (std::is_same<bool, T>::value) {/*..*/} };

Accounts

USERID | STATUS | LAST_LOGIN_DATE | etc...... 4594 . 1 . XX/XX/XX 00:00:00 . etc..... 2414 . 1 . XX/XX/XX 00:00:00 . etc..... 1894 . 1 . XX/XX/XX 00:00:00 . etc.....

History

我想USERID | VALUE | DATE 4594 . 10000 . XX/XX/XX 00:00:00 2414 . 10000 . XX/XX/XX 00:00:00 1894 . 10000 . XX/XX/XX 00:00:00 6974 . 10000 . XX/XX/XX 00:00:00 3491 . 10000 . XX/XX/XX 00:00:00 1590 . 10000 . XX/XX/XX 00:00:00 6554 . 10000 . XX/XX/XX 00:00:00 ,但要满足以下条件:对于每个SELECT Accounts.* FROM Accountsuserid列小于例如value。在MySQL中有可能吗?谢谢。

3 个答案:

答案 0 :(得分:2)

not exists是您想要的吗?

select a.*
from accounts a
where not exists (select 1
                  from history h
                  where h.userid = a.userid and h.value >= 30000
                 );

答案 1 :(得分:1)

您提到sum列中的value小于30000,对吗?

select a.*
from accounts a
where (
  select sum(h.value)
  from history h
  where h.userid = a.userid
) < 30000

或:

select a.*
from accounts a inner join (
  select userid
  from history
  group by userid
  having sum(value) < 30000
) t on t.userid = a.userid

修改
要对日期应用swcond条件:

select *
from accounts 
where userid not in (
  select userid
  from history
  where date >= date_sub(curdate(), interval 0 day)
  group by userid
  having sum(value) >= 30000
)

答案 2 :(得分:0)

您可以使用以下查询:

select accounts.* from accounts inner join history on history.userid = accounts.userid where history.value >= 30000

希望它对您有帮助。