2014-06-10 45 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)