[英]SQL Subquery empty return
I'm sorry for the title, but I didn't find a better one. 我很抱歉这个头衔,但我找不到更好的头衔。 My problem is the following: I want to select all fields from table "log" where field "user" is "user1" or "user2 in "friendships". My query looks like this, but it returns a zero value:
我的问题如下:我想从表“log”中选择所有字段,其中字段“user”是“user1”或“user2 in”friendships“。我的查询看起来像这样,但它返回一个零值:
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
If I used only one subquery it works, but I need the second field too. 如果我只使用一个子查询它可以工作,但我也需要第二个字段。 And there are more values (that's the reason why I use subqueries with ANY and not JOIN).
还有更多的值(这就是我使用ANY而不是JOIN的子查询的原因)。 I hope somebody finds my error :).
我希望有人发现我的错误:)。
i hope this works: 我希望这有效:
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: Can post the schema (or structure ) of your table? PS:可以发布表格的架构(或结构 )吗?
UPDATE 1 更新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
I would probably try a different approach, something like this perhaps: 我可能会尝试不同的方法,也许这样:
SELECT *
FROM `log`
WHERE `user` IN (
SELECT `user1` FROM `friendships` WHERE `user2` = @user AND `active` = 1
UNION ALL
SELECT `user2` FROM `friendships` WHERE `user1` = @user AND `active` = 1
UNION ALL
SELECT @user
)
GROUP BY
`arguments`
ORDER BY
`created` DESC
Although, to tell the truth, I would avoid selecting columns that are neither aggregated nor included in GROUP BY in a query like this, even though MySQL would allow me to do that. 虽然说实话,我会避免在这样的查询中选择既不聚合也不包含在GROUP BY中的列,即使MySQL允许我这样做。
Another alternative: 另一种选择:
SELECT *
FROM `log`
WHERE `user` IN (
SELECT
CASE `user1` WHEN @user THEN `user2` ELSE `user1` END AS `user`
FROM `friendships`
WHERE (`user1` = @user OR `user2` = @user) AND `active` = 1
UNION ALL
SELECT @user
)
GROUP BY
`arguments`
ORDER BY
`created` DESC
Refactor the OR into a single list using union: 使用union将OR重构为单个列表:
select *
from `log`
where `user` in (
select user1
from friendships
where user1 in (1, 2) and active)
union
select user2
from friendships
where `user1` in (1, 2) and active
)
group by arguments
order by `created` desc
or use a union on a standard join 或在标准联接上使用联合
select * from (
select l.*
from `log` l
join friendships f on l.user = f.user1 and f.user1 in (1, 2) and f.active
union
select l.*
from `log` l
join friendships f on l.user = f.user2 and f.user2 in (1, 2) and f.active
) x
group by arguments
order by `created` desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.