2017-06-06 122 views
1

我有一個小的問題,作爲條件的子查詢。條件語句中的MySQL子查詢

這是我試圖找出代碼:

SELECT `page` 
    FROM `mdl_tool_devcourse` 
    WHERE `page` IN 
      (SELECT `objectid` 
      FROM `mdl_logstore_standard_log` 
      WHERE `action` = 'viewed' AND `objecttable` = 'wiki_pages' AND `userid` = 2) 

      AND `ref` IN 

      (SELECT `objectid` 
      FROM `mdl_logstore_standard_log` 
      WHERE `action` = 'viewed' AND `objecttable` = 'wiki_pages' AND `userid` = 2) 

所以在我的WHERE語句我想都pageref是從我的子查詢的結果中。爲了達到這個目的,我找不到其他任何方式,而不是兩次運行子查詢。是否有另外一個選項讓子查詢只執行一次?

+0

您能否提供樣本數據和期望的結果?這看起來像一個不尋常的數據模型。 –

回答

1

子查詢超慢。嘗試運行該查詢:

SELECT 
    `mdl_tool_devcourse`.`page` 
FROM `mdl_tool_devcourse` 
INNER JOIN `mdl_logstore_standard_log` ON `mdl_logstore_standard_log`.`action` = 'viewed' 
    AND `mdl_logstore_standard_log`.`objecttable` = 'wiki_pages' 
    AND `mdl_logstore_standard_log`.`userid` = 2 
    AND `mdl_tool_devcourse`.`page` = `mdl_logstore_standard_log`.`objectid` 
    AND `mdl_tool_devcourse`.`page` = `mdl_logstore_standard_log`.`ref`