简体   繁体   English

MySQL查询在where子句中包含空值

[英]mysql query to include null values in where clause

I have a table member1 which looks like this. 我有一个像这样的表member1。 I am editing my table why I want to return all values from pst. 我正在编辑表,为什么要从pst返回所有值。

id  club_id mobile      pst
1     1     9437054788  6
2     1     9437345602  NULL
3     2     9437056738  35
4     2     9437064876  39
5     1     9437059927  NULL
6     2     9437965276  40
7     1     9437121455  NULL

I have a drop-down fields one for club_id and another check-box field for pst with a value of 'select All' on both fields. 我有一个下拉字段,一个用于club_id,另一个复选框字段用于pst,两个字段的值均为'select All'。 'select All' equals '%'. “全选”等于“%”。

The solution provided by @TheConstructor works well in this query @TheConstructor提供的解决方案在此查询中效果很好

SELECT group_concat(mobile) FROM `member1` WHERE COALESCE(pst, '') 
LIKE '%' and club_id=1

or 要么

SELECT group_concat(mobile) FROM `member1` WHERE COALESCE(pst, '') 
LIKE '6' and club_id=1

Now I want to have multiple values in the query in place of '6' for example 现在我想在查询中使用多个值代替“ 6”

SELECT group_concat(mobile) FROM `member1` WHERE COALESCE(pst, '') 
in (6,35,39,40) or in ('%') and club_id=1

How I can achieve that, as at the moment I can return the result with just one value ie 6 or 35 or 39 or 40 我如何实现这一目标,因为目前我只能返回一个值,即6或35或39或40

As you will have noticed NULL LIKE '%' will not evaluate to TRUE and rows where pst is NULL are excluded from the result. 如您所知, NULL LIKE '%'取值不会为TRUE并且结果中排除了pstNULL行。 Now there a different approaches to include rows where pst is NULL ; 现在有一种不同的方法可以包含pstNULL行; here are three of them: 这是其中的三个:

  1. If you want to get all rows you can just leave out the WHERE all together. 如果要获取所有行,则可以只将WHERE放在WHERE

     SELECT group_concat(mobile) FROM `member1`; 
  2. If you want to always include NULL you can add OR pst IS NULL . 如果要始终包含NULL ,则可以添加OR pst IS NULL (Can use index on column pst ) (可以在pst列上使用索引)

     SELECT group_concat(mobile) FROM `member1` WHERE pst LIKE '%' OR pst IS NULL; 

    If you plan on changing LIKE '%' for another predicate, this is probably the fastest SQL on bigger tables. 如果计划为另一个谓词更改LIKE '%' ,则这可能是较大表上最快的SQL。

  3. If you want to treat NULL as if it was an empty string you can use: (will not use index on column pst ) 如果您想将NULL视为空字符串,可以使用:(不会在pst列上使用index)

     SELECT group_concat(mobile) FROM `member1` WHERE COALESCE(pst, '') LIKE '%' 

    COALESCE(pst, '') will give the value inside column pst if it is not NULL or '' if pst is NULL . COALESCE(pst, '')会给柱内的值pst如果不是NULL''如果是PST NULL

    While I regard this to be the most flexible one as you only need to change '%' to something different to start filtering and exclude NULL -values, MySQL's optimizer will sadly handle this differently from the second query which will cost you performance on big tables where pst has an index. 虽然我认为这是最灵活的一种,因为您只需要将'%'更改为其他值即可开始过滤并排除NULL ,但可悲的是MySQL的优化程序将处理与第二个查询不同的操作,这将使您在大表上的性能降低其中pst有一个索引。

As a last caveat note that GROUP_CONCAT will by default only return 1024 characters. 最后要注意的是,默认情况下GROUP_CONCAT将仅返回1024个字符。 This can be changed by setting group_concat_max_len to a higher value. 可以通过将group_concat_max_len设置为更高的值来进行更改。

Forgive me if I've wildly misunderstood your question, but I think you want just: 如果我疯狂地误解了您的问题,请原谅我,但我想您只想:

SELECT group_concat(mobile) FROM member1

The % in WHERE pst LIKE '%' is a wildcard for zero or more of any character, thus the query will return any NOT NULL pst records.. you are way better off using WHERE pst IS NOT NULL if you need that logic. %WHERE pst LIKE '%'是零个或多个任意字符的通配符,因此查询将返回任何NOT NULL PST记录..你的方式最好使用WHERE pst IS NOT NULL ,如果你需要的是逻辑。

If you need to check the actual character % you'll need to escape it; 如果您需要检查实际字符% ,则需要对其进行转义; WHERE pst LIKE '\\%' .. but this is the same logic as WHERE pst = '%' as you are checking for equality. WHERE pst LIKE '\\%' ..但这与WHERE pst = '%'因为您正在检查是否相等。

I'm still struggling to understand you but if you are checking for existence in the database of a mobile number the simplest query is: 我仍在努力了解您,但是如果您要检查手机号码数据库中是否存在,最简单的查询是:

SELECT 1 FROM member1 WHERE mobile = ?

Where ? ? , is your pass in variable and any rows returned means the record exists. ,是您的传入变量,返回的任何行均表示该记录存在。 Make sure you use a binding method that is safe from SQL injection. 确保使用的绑定方法可以防止SQL注入。

I would also shy away from using GROUP_CONCAT in every case except the one when I am displaying the data as CSV. 除了将数据显示为CSV的情况以外,在每种情况下,我都不会使用GROUP_CONCAT If you are building a drop-down from the results.. just pull in the separate rows in an array and run through it as you build the HTML. 如果要从结果中构建下拉列表,则只需将数组中的单独行拉入并在构建HTML时对其进行遍历。

It seems you are using that value as-is, right from the form data. 看来您是直接从表单数据使用该值。 Are you sure this won't result in security issues? 您确定这不会导致安全问题吗?

I would grab the value from $_POST, test it, create a variable with the correct WHERE part of the query dependent on the test and use that in the query string. 我将从$ _POST中获取值,对其进行测试,然后根据测试依赖于查询的正确WHERE部分创建一个变量,并在查询字符串中使用该变量。

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

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