0

我正在尝试从名为“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
4

3 回答 3

1

表被命名blognews

比方说 $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
于 2013-10-17T05:44:36.330 回答
0

这不会很好地扩展,但您可以尝试:

SELECT * 
FROM news 
WHERE draft = '1' AND 
      (department_id = 1
      OR department_id = 4
      OR department_id = 5)
ORDER BY department_id DESC;
于 2013-10-17T05:47:24.270 回答
0

$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");
于 2013-10-17T05:48:51.163 回答