MySQL选择列表在数组中 [英] MySQL Select list In Array

查看:112
本文介绍了MySQL选择列表在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每篇文章可以有无限的类别.

Each Article can have unlimited categories.

类别会像这样保存在数据库中

Categories are saved in the database like this

例如article_category:"1","3","8"

example article_category: '1','3','8'

OR

例如article_category:"2","3"

example article_category: '2','3'

因此,当我在下面搜索列表时,可以说类别3

So when I search for a listing under, lets say category 3

$category_id = $_REQUEST['category_id'];  //3
$SQL = "SELECT * FROM ARTICLES WHERE article_category = '$category_id'";

如果只是其中之一,则可以正常工作.

If it were just one the above would work fine.

我不想这么说,但是我完全迷路了.我会使用IN吗?

I hate to say it but I'm completely lost.. Would I use IN ?

推荐答案

=(等于)检查完整字符串.您可以使用LIKE和%comodin完成操作(对于*在dir之类的文件,它就像*)

= (equals) check against the complete string. what you want to do could be done using LIKE and the % comodin (it's like * for files in dir or the like)

$SQL = "SELECT * FROM ARTICLES WHERE article_category LIKE \"%'$category_id'%\"";

但是,我建议您规范化数据库并将类别作为子表

however, i reccommend that you normalize the database and have categories as a subtable

这篇关于MySQL选择列表在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆