dongtui6347 2019-07-04 15:59
浏览 54

MySQL - 如果列的数组值中包含数字,则获取一行[重复]

I have been racking my brain looking for an answer to this very simple question.

I have two tables with data in them. The first table contains a group of categories and their id. The second table contains a group of posts that can be linked to each category. Now I need to select the posts that are in each category, but it's not working the way I expect. I have tried using the IN operator and it works on the first value in the rows then doesn't on the rest.

Here is how the tables are roughly structured

Category Row Structure:

+===================+
| cID  |   cName    |
+===================+
|  1   |  cars      |
|  2   |  trucks    |
|  3   |  kittens   |
|  4   |  quads     |
+===================+

Post Structure:

+==========================================+
| pID  |   title    |  content  |   cats   |
+==========================================+
|  1   |  a post 1  | text here | 1,3,4    |
|  2   |  a post 2  | text here | 2,3,4    |
|  3   |  a post 3  | text here | 1,4      |
|  4   |  something | text here | 2        |
+==========================================+

When I visit the page for the cars category, it runs a query similar to this

$showFrom = 1; // THE cID of the Cars category
query_function("SELECT .. FROM entries WHERE cats IN (?)", array($showFrom));
// Query function just prepares the statement and passes the variables to the function

With this function I see a post 1 and a post 3 show up. Great. But if I go to the kittens or quads page, I am not seeing these posts. It stops reading after 1 in the first and 3rd item, and 2 on the second and fourth item.

I considered using the CONTAINS function, but if they have more then 9 categories, and you visit category 1, you would see category 10 and 11 and so on as they would CONTAIN the literal value of 1 right? Or am I missing something here?

Thanks

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么