2013-04-25 85 views
0

我是mysql數據庫的新手,我試圖創建一個使用連接的查詢。 我已經寫了,走的時間來執行1秒,當前查詢,我已經做了reasearch,發現加入成爲減少執行的時間最好的解決辦法,轉換mysql子查詢加入

我在用的查詢是:

SELECT count(1) as total 
FROM `mytable` 
where pid IN (
    SELECT pid FROM `maps` 
    where `node_id` = (
      SELECT nid FROM shop 
      WHERE value = 'selling' 
      and parent = '170' 
      and tid = 1 
     ) 
    and tid = 1 
) 
and `pnumber` IN (
    select pnumber 
    FROM users_pro 
    where value = 'hulo' 
    and tid = 1 
) 
and tid = 1 

請大家不要多多幫助。

謝謝..

+1

嗯,'限1'沒有'ORDER BY' ......對我來說,這意味着 '一個隨機記錄',你有你'nid'其他要求? – Wrikken 2013-04-25 20:08:27

+0

@Wrikken對於nid – Afsar 2013-04-25 20:11:09

+0

,請僅說明一點:如果內部查詢中有兩個或更多不同的'nid'滿足要求,該怎麼辦? – Wrikken 2013-04-25 20:13:18

回答

1
SELECT count(distinct mytable.*) as total 
FROM mytable 
JOIN maps 
    ON mytable.pid = maps.pid 
    AND maps.tid = mytable.tid 
JOIN shop 
    ON maps.node_id = shop.nid 
    AND shop.value = 'selling' 
    AND shop.parent = '170' 
    AND shop.tid = mytable.tid 
JOIN users_pro 
    ON mytable.pnumber = users_pro.pnumber 
    AND users_pro.value = 'hulo' 
    AND users_pro.tid = mytable.tid 
WHERE mytable.tid=1 
+0

謝謝你的完美作品。 – Afsar 2013-04-26 14:57:05