我想从下面的查询2中选择WHERE (uid = {$uid} OR uid = **HERE** )
其中**HERE**
为cid
的所有行。
查询1:
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` = **HERE** )
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
查询2:
SELECT `cid` FROM `t_con` WHERE `uid` = {$uid} AND `flag` = 1
答案 0 :(得分:2)
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` in (SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1))
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
答案 1 :(得分:1)
您也可以将其作为联接:
SELECT *
FROM `t_activities` ta left outer join
(SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1)
) tc
on ta = tc.cid
WHERE (`uid` = {$uid} OR tc.`uid` is not null) AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
顺便说一下,作为一个SQL语句,“GROUP BY fid”看起来很奇怪。这在mysql中是允许的,但我认为这是一个不好的做法。明确你正在做的事情要好得多:
SELECT fid, min(<field1>) as Field1, . . .
这有助于防止在您返回查询或尝试修改查询时出错。