【问题标题】:how to use where condition from stored data in array如何使用数组中存储数据的where条件
【发布时间】:2015-04-22 00:18:03
【问题描述】:
id | name | permission
1  | John | 1,4,3
2  | mike | 7,4,3
3  | sky  | 3,2,1

这是我的数据库

现在我已经获取带有 where 条件的选择查询

例如select * from friend where permission='4'

但我无法获取任何数据,该怎么办?

请帮忙

【问题讨论】:

  • 不要这样存储权限值,只会给你带来很多问题。每个权限都有一行!而且,还要使用正确的数据类型,在这种情况下是整数!
  • find_in_set 或类似的 mysql 查询。
  • 规范化就是为了这个目的!在不同的行中存储不同的权限
  • 无论如何,权限选项卡是列表还是普通字符串?

标签: php mysql sql database where-in


【解决方案1】:

Select * fromfriend where FIND_IN_SET('4',permission);

【讨论】:

  • 如果您发现它对您有用,请接受它作为答案。谢谢!
【解决方案2】:

看起来您有多个权限一起存储在一个字符串中。

对你来说,查询看起来像:

select * from friend where permission='4'

这意味着任何包含 4 的东西。 对mysql来说,它看到:

select * from friend where permission= ONLY '4'
// Meaning the permissions column can ONLY CONTAIN 4.
// Also, ONLY is meant as a visual, don't use it in queries.

试试:

find_in_set('4',permission) <> 0
// This means It needs to find number 4 and can't return 0 results.

查看这些以了解更多信息:

MySQL query finding values in a comma separated string

http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php

【讨论】:

    【解决方案3】:

    可以通过以下查询来完成:

    select * from friend where permission like '%4%'
    

    【讨论】:

    • 这是一个不好的例子,因为它允许任何带有 4 的东西。因此,如果由于某种原因有权限 4 和 14,它会允许两者,即使他们只想要 4。这是因为它只是从您编写它的方式中找到带有 4 的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多