0

我有这个简单的查询:

SELECT DISTINCT images.id
FROM `images`
WHERE `images`.`user_id` IN
    (SELECT following_id
     FROM `follows`
     WHERE `follows`.`follower_id` = 5717
       AND `follows`.`accepted` = 1)
ORDER BY id DESC LIMIT 1000
OFFSET 0

我想将一个值附加到子查询返回的值列表中。因此,如果子查询返回:

23
59
14
77

我希望它是:

23
59
14
77
86

(注意底部的额外 86)。

4

2 回答 2

3

如果您总是希望添加 86,那么只需在子查询上使用 UNION ALL,这将确保 86 始终包含在您的 IN 子句子查询中:

SELECT DISTINCT spentits.id
FROM `spentits`
WHERE `spentits`.`user_id` IN
    (SELECT following_id
     FROM `follows`
     WHERE `follows`.`follower_id` = 5717
       AND `follows`.`accepted` = 1
     UNION ALL
     SELECT 86)
ORDER BY id DESC LIMIT 1000
OFFSET 0
于 2013-06-20T21:55:37.210 回答
0

您也可以将其添加为 WHERE 子句中的第二条标准:

SELECT DISTINCT spentits.id
FROM `spentits`
WHERE `spentits`.`user_id` IN
    (SELECT following_id
     FROM `follows`
     WHERE `follows`.`follower_id` = 5717
       AND `follows`.`accepted` = 1)
   OR `spentits`.`user_id` = 86
ORDER BY id DESC LIMIT 1000
OFFSET 0
于 2013-06-20T21:56:12.427 回答