我正在尝试从名为“blog”的表中选择具有正确部门 ID 的行。department_id 字段看起来像“1,4,5”。现在,如果站点编号 1、4 或 5 查询,他们应该会收到该帖子,因为他们的编号在该字段中。但我遇到了麻烦。这是我的代码,因此非常感谢任何帮助。
SELECT * FROM news WHERE draft = '1' AND department_id IN ($id) ORDER BY id DESC
表被命名blog
或news
?
比方说 $id=3;
SELECT * FROM news WHERE draft = 1 AND $id IN (department_id) ORDER BY id DESC
或者您也可以使用
SELECT * FROM news WHERE draft = 1 AND find_in_set('$id', cast(department_id as char)) > 0 ORDER BY id DESC
这不会很好地扩展,但您可以尝试:
SELECT *
FROM news
WHERE draft = '1' AND
(department_id = 1
OR department_id = 4
OR department_id = 5)
ORDER BY department_id DESC;
$id是什么?如果 $id 是数组,您需要制作字符串,例如
$id = array(1,4,5);
$ids = implode(',', $id);
mysql_query("SELECT * FROM news WHERE draft = '1' AND department_id IN ($ids) ORDER BY id DESC");