简体   繁体   English

MySQL Select陈述式,WHERE'IN'子句

[英]MySQL Select Statement, WHERE 'IN' Clause

I currently have the following row in my table: 目前,我的表中包含以下行:

         course_data:
             user_id        days     <-- This is a varchar column.
               405          1,3,5

and I am trying to implement the following SELECT statement: 并且我正在尝试实现以下SELECT语句:

SELECT usrID, usrFirst, usrLast, usrEmail
    FROM tblUsers
    WHERE usrID NOT IN
    (
        SELECT users.usrID
            FROM
                `course_data` courses,
                `tblUsers` users
            WHERE
                days IN ('$day')
    )
    GROUP BY usrID
    ORDER BY usrID

Basically, I want that row (with user 405) to be omitted if the $day variable includes a '1, 3, or 5'. 基本上,如果$ day变量包含“ 1、3或5”,我希望省略该行(与用户405一起使用)。

For example, if $day = "1" , it should return an empty query (because the number "1" is in the column "1,3,5"). 例如,如果$day = "1" ,它将返回一个空查询(因为数字“ 1”在列“ 1,3,5”中)。

However, I have not found this to be the case. 但是,我还没有发现这种情况。 Even though $day = "1" , it still returns that row. 即使$day = "1" ,它仍然返回该行。

The only way that it won't return the row is if $day= "1,3,5." 它不会返回该行的唯一方法是$day= "1,3,5." Yet, I thought that the IN() clause would take any part of my variable and apply it to that column. 但是,我认为IN()子句会占用变量的任何部分并将其应用于该列。

Any insights on what I'm doing wrong here? 对我在这里做错的任何见解吗? Thanks. 谢谢。

Remove the Quotes in the IN Statement. 删除IN语句中的引号。 The Syntax is: 语法为:

... WHERE column IN (1,2,3) 

and not as you used it 而不是像你以前那样

... WHERE column IN ('1,2,3')

Also see the documentation on IN, there are more examples. 另请参阅有关IN的文档 ,还有更多示例。

You should use the like keyword to do a partial match of the char field: 您应该使用like关键字对char字段进行部分匹配:

where days like '%1%'

Edit: VolkerK and Lukas Lalinsky both suggest MySQL's find_in_set , which is probably better than like for what you want to do. 编辑:VolkerK和卢卡斯·拉林斯基都表明MySQL的find_in_set ,这可能是优于like你想要做什么。 However, the following recommendation about normalizing your database still applies. 但是,以下有关规范化数据库的建议仍然适用。

However, you should not store multiple values in a single database field. 但是,您不应在一个数据库字段中存储多个值。 Instead, consider using two tables: 而是考虑使用两个表:

course_data:
    user_id

course_days:
    user_id
    day_number

Then, you would have the following data: 然后,您将拥有以下数据:

course_data:
    user_id
    405

course_days
    user_id    day_number
    405        1
    405        3
    405        5

You can then correctly query this schema. 然后,您可以正确查询此架构。 For example: 例如:

select  cd.user_id
from    course_data as cd
where   cd.user_id not in
    (
        select  course_days.user_id
        from    course_days
        where   course_days.user_id = cd.user_id
            and course_days.day_number = 1
    )

(or, that should be close; I'm not exactly sure what you're trying to accomplish or what the rest of your schema looks like, so this is a best guess). (或者,这应该是接近,我不完全相信你想要什么来完成或者什么休息您的架构看起来像,所以这是一个最好的猜测)。

I think the query you want is: 我认为您想要的查询是:

SELECT usrID, usrFirst, usrLast, usrEmail
FROM tblUsers
WHERE usrID NOT IN (
    SELECT user_id 
    FROM course_data
    WHERE find_in_set(?, days) > 0
)
ORDER BY usrID

But you should seriously consider normalizing the database, so that each day has it's own row. 但是您应该认真考虑对数据库进行规范化,以便每一天都有自己的行。

If I understand your question correctly you want the contents of the varchar field to be treated as a comma-separated list and test whether this list does not contain a certain value. 如果我正确理解了您的问题,则希望将varchar字段的内容视为以逗号分隔的列表,并测试此列表是否不包含某个值。 For that you need the find_in_set(str, strlist) function. 为此,您需要find_in_set(str,strlist)函数。
But keep in mind that MySQL can't use an index in that case and your query will always need a full table scan. 但是请记住,在这种情况下,MySQL无法使用索引,并且您的查询将始终需要全表扫描。 It might be better not to store structured data (and run comparisons on the single elements) in a single column but to use another table and a JOIN as has been suggested in other responses. 可能是最好不要(对单一要素和运行比较),结构化数据存储在一个单一的列,但使用另一个表和已在其他回应有人提出一个JOIN。

days does not hold a list of strings. 天不包含字符串列表。 It holds a single string. 它包含一个字符串。 Is the string "1,3,5" inside one of the strings in {'1'}? 字符串“ 1,3,5”是否位于{'1'}中的字符串之一中? Clearly the answer is no. 显然答案是否定的。 Either refactor days into a different table, or be prepared to do more string manipulation 要么将天重构到另一个表中,要么准备进行更多的字符串操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM