简体   繁体   中英

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:

    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). 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?

UPDATE 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM