0

我有这个查询

SELECT *
FROM `posts`
WHERE `language` IN ( SELECT `possible_languages` FROM `users` WHERE user_id = ? );

In the table posts: languages can be either enor ar
In the table users: possible_languagescan be either 'en', 'ar'or 'en','ar'
When the selected value is 'en','ar'the query doesn't work
I want it to be able to select Posts that are either enor arIN ('en','ar ')

4

2 回答 2

3

您不能in与此类列表一起使用。你可以这样做exists

SELECT p.*
FROM `posts` p
WHERE exists (select 1
              from `users` u
              where u.user_id = ? and find_in_set(p.language, u.possible_languages)
             )

如果你有一个索引,这将更有效users(user_id, possible_languages)

于 2013-08-06T11:49:16.650 回答
1

作为连接可能更有效

SELECT posts.*
FROM `posts`, `users`
WHERE find_in_set(`language`,`possible_languages`)
AND user_id = ?

然而,对此更有效的查询可能是:

SELECT posts.*
  FROM posts,users
 WHERE (language=possible_languages OR possible_languages='ar,en')
   AND user_id=?

假设 language 和 possible_languages 中的值仅符合您的指定。在这种情况下,帖子中的语言索引很有用。

于 2013-08-06T11:49:30.797 回答