抱歉,我没找到更好的标题。
我的问题如下:
我想从表“ log”中选择所有字段,其中“ user”字段是“ user1”或“ friendships”中的“ user2”,我的查询看起来像这样,但返回零值:
SELECT * FROM `log`
WHERE `user` = ANY(SELECT `user1` FROM `friendships`
WHERE (`user1` = 1 OR `user2` = 1) AND `active` = 1)
OR `user` = ANY(SELECT `user2` FROM `friendships`
WHERE (`user1` = 1 OR `user2` = 1) AND `active` = 1)
GROUP BY `arguments`
ORDER BY `created` DESC
如果仅使用一个子查询,则可以,但是我也需要第二个字段。还有更多的值(这就是为什么我将子查询与ANY一起使用而不是JOIN的原因)。
我希望有人发现我的错误:)。
最佳答案
我希望这可行:
SELECT *
FROM `log` INNER JOIN `friendships` on
`log`.`user` = `friendships`.`user`
WHERE `friendships`.`user` in (1,2)
AND
`friendships`.`active` = 1
ORDER BY `created` DESC
PS:可以发布表的架构(或结构)吗?
更新1
SELECT *
FROM
(
SELECT *
FROM `log` INNER `friendships` on
`log`.`user` = `friendships`.`user1`
WHERE `friendships`.`user1` in (1,2)
AND
`friendships`.`active` = 1
UNION
SELECT *
FROM `log` INNER `friendships` on
`log`.`user` = `friendships`.`user2`
WHERE `friendships`.`user2` in (1,2)
AND
`friendships`.`active` = 1
) as iTable
ORDER BY iTable.`created` DESC