2015-10-09 106 views
1

我知道這是一個非常愚蠢的問題,並相信我,我嘗試了一切。這是我最後的手段。Mysql子查詢返回不正確的結果

我想運行一個子查詢。令人驚訝的是,MySQL掛起(即使它不是一個大的查詢)。

我試圖運行該查詢返回我Link_ids

select distinct link_id from join_link_object where obj_id = (select group_concat(obj_id) from xalt_object where module_name like '%intel-compilers/2016.0.047%'); 

+---------+ 
| link_id | 
+---------+ 
| 153249 | 
+---------+ 

列表返回的結果是不正確的。而不是返回link_id查詢列表只返回第一個。

我知道使用=不是一個好主意,我嘗試使用IN,但提示只是掛起並沒有返回任何東西。

儘管分別運行子查詢,然後運行主查詢及其結果可獲得正確的結果(手動驗證)。

mysql> select group_concat(obj_id) from xalt_object where module_name like '%intel-compilers/2016.0.047%'; 
+-------------------------------------------+ 
| group_concat(obj_id)      | 
+-------------------------------------------+ 
| 352304,352305,352306,352307,352308,354813 | 
+-------------------------------------------+ 

然後運行主查詢 -

mysql> select distinct link_id from join_link_object where obj_id in (352304,352305,352306,352307,352308,354813); 
+---------+ 
| link_id | 
+---------+ 
| 153249 | 
| 153467 | 
| 153996 | 
| 154170 | 
| 155077 | 
| 155099 | 
| 155100 | 
+---------+ 

我甚至嘗試使用存在,但這些查詢返回荒謬的結果(約156995行) -

select distinct link_id from join_link_object where EXISTS (select obj_id from xalt_object where module_name like '%intel-compilers/2016.0.047%'); 

請指點!由於

+0

在第一個查詢中,使用'where obj_id ='在倒數第二個查詢中使用'where obj_id in'嘗試在第一個查詢中使用'in' –

回答

2

在你與子查詢結果比較obj_id第一次查詢的時候你應該過濾obj_id in子查詢的結果,因爲你在測試所做的:

select distinct link_id 
from join_link_object 
where obj_id in (352304,352305,352306,352307,352308,354813); 

下面是它應該是:

select distinct link_id 
from join_link_object 
where obj_id 
in (select obj_id from xalt_object where module_name like '%intel-compilers/2016.0.047%'); 

爲了提高性能,嘗試了這一點:

SELECT DISTINCT jlo.link_id 
FROM join_link_object AS jlo 
INNER JOIN xalt_object AS xo 
ON (jlo.obj_id = xo.obj_id) 
WHERE xo.module_name LIKE '%intel-compilers/2016.0.047%'; 
+0

正如我所說的,我嘗試了IN,但整個事情只是掛起。 – user3802162

+0

我看,問題是'group_concat' –

+0

聖牛!嚴重的是,我認爲我處於那個我無法思考的階段。感謝這工作。但整個查詢需要7.17秒,這是obj_id列表非常小的時候。我想知道是否有一種方法我可以做得更快。 – user3802162