2014-06-10 46 views
1

我想表A登记的数量都在表B.确定的时间具体数额的MySQL从表中选择其中条件x和子查询

我要找的正确的方式做一些像此:

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) 

基本上,表-B由3米栏:命名的“id”的int主键,一个名为“开始”的日期时间,并命名为“结束”的其他日期时间。这是我确定今年所有周的方式。

+1

你错过了从第二个选择查询条款... – ravikumar

+0

谢谢!我没有想到会接近解决方案! – Alberto

回答

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) 
+0

嗯,它是*一种方式*。不确定它是否是正确的方式! – Strawberry

+0

哦,我的意思是它工作:) – Alberto

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) 
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; 
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)