mysql从表中选择条件x和subselect

时间:2014-06-10 12:51:04

标签: mysql select

我想将表A中的注册管理机构数量计算到表B中确定的特定时间。

我正在寻找这样做的正确方法:

SELECT count(*) as total
from table_a as a
WHERE a.created >= '0000-00-00 00:00:00'
AND a.created <= (SELECT table_b.end WHERE table_b.id = 13)

基本上,table_b由3列组成:名为'id'的int主键,名为'start'的日期​​时间,以及名为'end'的其他日期时间。这是我确定当年所有周的方式。

4 个答案:

答案 0 :(得分:2)

万一有人需要,这是正确的方法:

SELECT count(*) as total
from table_a as a
WHERE a.created >= '0000-00-00 00:00:00'
AND a.created <= (SELECT table_b.`end` FROM table_b WHERE table_b.`id`=13)

答案 1 :(得分:1)

您可以进行典型的加入:

SELECT COUNT(*) AS total
FROM table_a AS a 
JOIN table_b as b ON (a.created >= '0000-00-00 00:00:00' and a.created <= b.`end`)
WHERE b.id=13;

根据您可能想要的描述:

SELECT COUNT(*) AS total
FROM table_a AS a 
JOIN table_b as b ON (a.created >= `start` and a.created <= `end`)
WHERE b.id=13;

答案 2 :(得分:0)

SELECT count(*) as total
from table_a as a
WHERE a.created >= '0000-00-00 00:00:00'
AND a.created <= (SELECT table_b.end from table_b WHERE table_b.id = 13)

答案 3 :(得分:0)

您可以使用BETWEEN子句来实现此目的,

SELECT count(*) as total
from table_a a
WHERE a.created BETWEEN '0000-00-00 00:00:00'
AND (SELECT table_b.`end` FROM table_b WHERE table_b.`id`=13)