【发布时间】:2012-07-13 18:21:12
【问题描述】:
我在 mysql 数据库中有 2 个表存储和共享。我试图避免使用 IN 子句。详情请看下文
select id, user_id from stores where user_id =7;
+----+---------+
| id | user_id |
+----+---------+
| 36 | 7 |
| 37 | 7 |
select stores_id,share_id from shares where share_id=7;
+-----------+----------+
| stores_id | share_id |
+-----------+----------+
| 15 | 7 |
| 38 | 7 |
现在我运行这个
SELECT stores.id
FROM stores
WHERE user_id = 7
UNION
(SELECT stores.id
FROM stores
WHERE id IN (SELECT stores_id
FROM shares
WHERE share_id = 7));
要得到以下结果:
+----+
| id |
+----+
| 36 |
| 37 |
| 15 |
| 38 |
+----+
问题 如何重写查询以便不使用 IN 关键字?
【问题讨论】:
-
是的……当我不知道的时候。 :)
标签: mysql sql join conditional