我有桌子说 abc
AT+CMGF
另一个表 non_admin_users
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
我的Sql查询是
id
3
33940
33997
33998
33999
此查询返回重复记录。
我想要的是从abc表中获取所有记录,其smownerid等于non_admin_users' id或其smcreatorid为33998,不使用where子句或不同的id列或group by语句。
我想要以下输出
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
)
答案 0 :(得分:1)
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)
答案 1 :(得分: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;
答案 2 :(得分: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 |
+------+-----------+-------------+