如果我写这样的查询是好的: - (请参阅where where condition中的查询)
SELECT distinct(id) "idea_id"
FROM ideas
WHERE deleted_by_user = 0 AND moderation_flag = 1 AND
user_id in (select id
from users
where confirm like "yes")
ORDER BY time_of_creation DESC
如果此查询中存在某些问题,请与我们联系: 提前谢谢..
答案 0 :(得分:0)
SELECT distinct i.id "idea_id"
FROM ideas i join users u
on i.user_id=u.id and u.confirm ='yes'
WHERE i.deleted_by_user = 0
AND i.moderation_flag = 1
ORDER BY i.time_of_creation DESC
答案 1 :(得分:0)
SELECT distinct a.ID idea_id
FROM ideas a
INNER JOIN users b
ON a.user_id = b.id
WHERE a.deleted_by_user = 0 AND
a.moderation_flag = 1
b.confirm = 'YES'
ORDER BY time_of_creation DESC
答案 2 :(得分:0)
您可以通过两种方式编写此查询:
SELECT DISTINCT(i.id) "idea_id"
FROM ideas i
INNER JOIN users u ON i.user_id = u.id
WHERE i.deleted_by_user = 0 AND i.moderation_flag = 1 AND u.confirm = 'yes'
ORDER BY i.time_of_creation DESC;
并且
SELECT DISTINCT(i.id) "idea_id"
FROM ideas i
WHERE i.deleted_by_user = 0 AND i.moderation_flag = 1 AND
EXISTS (SELECT * FROM users u WHERE i.user_id = u.id AND u.confirm = 'yes')
ORDER BY i.time_of_creation DESC;
答案 3 :(得分:0)
要回答您的问题 - 使用子查询没有问题。
另一方面,在以某种方式编写查询时,您至少要考虑三种不同的事情:
如果您有一个效率很重要的数据库,那么选择如何制定查询的最佳方法通常是以不同的方式编写它并在数据库上测试它。 (但通常数据库中的查询优化器非常好,没关系)