2017-08-10 65 views
0

我已經表說ABC多個列內連接返回重複記錄

id smownerid smcreatorid 
1 33997   1 
2 33997   1 
3 33997   1 
4 33998   1 
5 33998   1 
6 33998   33998 
7 33998   33998 
8 33998   33998 
9 33998   33998 
10 33940   33998 
11 33940   33998 
12 3    33998 
13 33940   1 
14 33940   33998 
15 33997   33997 
16 3    33997 
17 33940   33998 
18 4    33940 
19 4    33997 
20 33980  33940 

另一個表non_admin_users

id 
3 
33940 
33997 
33998 
33999 

我的SQL查詢

SELECT abc.* from abc 
INNER JOIN non_admin_users vt_tmp_u33998 ON vt_tmp_u33998.id = abc.smownerid 
OR (
vt_tmp_u33998.id = abc.smcreatorid 
AND vt_tmp_u33998.id =33998 
) 

這個查詢返回重複 記錄。

我想要的是從abc表中獲取所有記錄,其smownerid等於non_admin_users的id或其smognorid爲33998,而不使用where子句或distinct列或group by語句。

我想這下輸出

id smownerid smcreatorid 
1 33997   1 
2 33997   1 
3 33997   1 
4 33998   1 
5 33998   1 
6 33998   33998 
7 33998   33998 
8 33998   33998 
9 33998   33998 
10 33940   33998 
11 33940   33998 
12 3    33998 
13 33940   1 
14 33940   33998 
15 33997   33997 
16 3    33997 
17 33940   33998 
+0

這是一個有點混亂,你的查詢是指在問題(vtiger_crmentit)中沒有報價表,爲什麼棘手的別名非admin_users? –

+0

對不起,查詢。加入non_admin_users是必要的。 –

回答

0
INNER JOIN non_admin_users vt_tmp_u33998 ON (vt_tmp_u33998.id = abc.smownerid AND abc.smcreatorid !=33998) OR (vt_tmp_u33998.id = abc.smcreatorid AND vt_tmp_u33998.id =33998) 
+0

我很困惑smcreatorid = 33998。如果你只提到on_admin_users.id = 33998而不是smcreatorid,那麼它會容易得多。我認爲abc表可以有on_admin_users表中不存在的記錄,我相信我們發佈答案的所有人都有類似的想法,因此所有發佈的代碼都集中在abc表的smcreator字段,否則他們可能會只需做一個簡單的選擇和連接,而不需要像你所做的那樣的子查詢。我可能是錯誤的 – Learning

0

試試這個:

SELECT a.smownerid, a.smcreatorid 
FROM abc a JOIN non_admin_users na ON a.smownerid = na.id 
WHERE a.smownerid = 33998; 

,如果你想所有的記錄,您可以註釋掉WHERE條件。

+0

我想要從abc表中獲取所有記錄,其** ** smownerid **等於non_admin_users的** id **或smcreator等於33998. –

0

你可以試試嗎?

SELECT a.* 
FROM abc a LEFT JOIN non_admin_users na ON a.smownerid = na.id 
WHERE a.smcreatorid = 33998 OR IFNULL(na.id,'') != ''; 

[或]只是爲了確保33998記錄不會連接兩次。

select * from 
(SELECT a.* 
FROM abc a INNER JOIN non_admin_users na ON a.smownerid = na.id AND a.smcreatorid!=33998 
UNION ALL 
SELECT b.* FROM abc b INNER JOIN abc b1 ON b.id=b1.id AND b.smcreatorid=33998) a 
order by id; 
+0

OP狀態「不使用where子句」.. –

0

也許一個隱含的不同之處在工會是確定

select * from 
(
SELECT a.id, A.smownerid, A.smcreatorid 
FROM ABC a 
INNER JOIN non_admin_users n on n.id = a.smownerid and a.smcreatorid <> 33998 
union 
SELECT a.id, A.smownerid, A.smcreatorid 
FROM ABC a 
inner JOIN non_admin_users n on n.id = a.smownerid 
) s 
order by id 
; 

+------+-----------+-------------+ 
| id | smownerid | smcreatorid | 
+------+-----------+-------------+ 
| 1 |  33997 |   1 | 
| 2 |  33997 |   1 | 
| 3 |  33997 |   1 | 
| 4 |  33998 |   1 | 
| 5 |  33998 |   1 | 
| 6 |  33998 |  33998 | 
| 7 |  33998 |  33998 | 
| 8 |  33998 |  33998 | 
| 9 |  33998 |  33998 | 
| 10 |  33940 |  33998 | 
| 11 |  33940 |  33998 | 
| 12 |   3 |  33998 | 
| 13 |  33940 |   1 | 
| 14 |  33940 |  33998 | 
| 15 |  33997 |  33997 | 
| 16 |   3 |  33997 | 
| 17 |  33940 |  33998 | 
+------+-----------+-------------+ 
+0

ABC表中應該有記錄的拉赫。工會將放慢績效。 @ P.Salmon –