0
我有3個表格。MySQL Select EXISTS計數
表1 ID,thing_id
table_index ID
table_index_info table_index_id,table1_id
table_index_info包含table_index的歷史。這個歷史可以引用table1,可能多次或0次。
我需要運行一個查詢,該查詢返回table1中所有具有特定thing_id的行。 它還需要計算table_index中至少有一個table_index_info鏈接到table1的行數。
這裏是我的查詢:
SELECT table1.*,
(SELECT COUNT(i.id)
FROM table_index i
WHERE EXISTS (SELECT 0
FROM table_index_info h
WHERE h.table1_id = table1.id
AND h.table_index_id = i.id)
) AS indexCount
FROM table1
WHERE table1.thing_id= $thingId
這是做到這一點的最好/正確的方法是什麼?
如果table_index_info上有許多結果,JOIN會不會變慢? – Ethan 2011-05-25 18:39:27
我只是跑了一些測試,他們都工作,但是當table_index_info中有很多行EXISTS查詢需要永久和JOIN查詢是快速的..你能解釋這對我嗎? – Ethan 2011-05-31 17:35:23