【问题标题】:Select SQL with spaces in value选择值中有空格的 SQL
【发布时间】:2014-05-27 04:11:16
【问题描述】:

我有一个这样的 php 文件:

<?php define('AREA', 'C');
    require ('prepare.php');
    require ('init.php');
    $con = mysql_connect(Registry::get ( 'config.db_host' ), Registry::get ( 'config.db_user' ), Registry::get ( 'config.db_password' ))or die("Unable to connect to MySQL");
    mysql_select_db(Registry::get ( 'config.db_name' ));
     if (!empty($_POST['dealer_company']))
     {
         $dealerCompany = 'Your company'; //$_POST['dealer_company'];                
         $result = mysql_query("SELECT distinct a.firstname, a.lastname, a.email, a.user_id
     FROM cscart_users a,cscart_usergroup_links b, cscart_usergroup_descriptions c
     WHERE b.usergroup_id=c.usergroup_id and b.status = 'A' and a.user_id=b.user_id and   
         c.usergroup='Dealer' and a.company = 'Your company'",$con) or die(mysql_error());

        while($row = mysql_fetch_array($result))
        {
            $d_Infos[]=$row;            
        }
     }
     if (!empty($_POST['dealer_company']))
     {
        $dealerInfos = '<select id="as_dealer_id" name="as_dealer_id" class="input-text">
            <option value="">Please select</option>';

                        foreach ($d_Infos as $k => $v)
                        {
              $dealerInfos .= '<option value="'.$v['user_id'].'">'.$v['firstname'].'</option>';                
            } 
     $dealerInfos .= '</select>';
         echo $dealerInfos;
      }
?>

如您所见,我在 SQL 查询中有一个条件是 a.company = 'Your company'。 但它不能选择具有该值的列。结果是空的。如果“您的公司”中没有空格,它将起作用,例如:“您的公司”。 所以我的问题是: 是否可以选择条件包含空格的行? 非常感谢。

【问题讨论】:

  • 您可以尝试使用 PHP 的 trim() 函数,这是一种方法。我确定有一个 SQL 等价物,但我不记得它是什么了。所以试试$dealerCompany = trim('Your company '); 然后and a.company = '".$dealerCompany."'
  • @Fred-ii- 你喝醉了。是a.company = 'Your company' 没问题,你确定数据库值吗?
  • 感谢您的帮助,但问题是我的数据库中的数据也包含空间。在公司栏中,它也是“您的公司”。 :(
  • 我测试了一段类似的代码,我在这里玩过,使用trim() 效果很好。它将删除在 POST 变量的左侧和/或右侧找到的任何空格。即:$var=trim($_POST['var']);
  • 更具体地说是$dealerCompany = trim($_POST['dealer_company']);,专为您定制。

标签: php mysql sql sql-server


【解决方案1】:

将以下内容与 PHP 的 trim() 函数一起使用。

$dealerCompany = trim($_POST['dealer_company']);

还有:

and a.company = '".$dealerCompany."'

这将“修剪”左侧和/或右侧的任何空格。

您也可以在将数据插入数据库时​​使用相同的功能。如果有人在表单输入元素的开头和/或结尾处输入一个或多个空格,这将确保条目的左侧和/或右侧不包含空格。

  • 最好立即执行此操作以避免并发症。

【讨论】:

    【解决方案2】:

    你能试试这个吗,已添加a.company LIKE '%Your company%'

     $result = mysql_query("SELECT distinct a.firstname, a.lastname, a.email, a.user_id
                                   FROM cscart_users a,cscart_usergroup_links b, cscart_usergroup_descriptions c
                                   WHERE b.usergroup_id=c.usergroup_id and b.status = 'A' and a.user_id=b.user_id and c.usergroup='Dealer' and a.company LIKE '%Your company%'",$con) or die(mysql_error());
    

    【讨论】:

    • 感谢您的帮助。我试过了,但行不通。这真的很奇怪:(
    • mysql_query?没有人会告诉他使用 mysqli 或 pdo?
    【解决方案3】:

    您可以在数据的第一个和最后一个位置添加通配符字符串 (%)。但它也不搜索字符串。

    所以有两种可能性是你把所有的空间都修剪到两边

    where replace(columnname ,' ','') = '''%'+ replace(@value,' ','') + '%'''
    
    select REPLACE ('abadf adsfa',' ','')
    

    或 第二种选择是使用 FTS(全文搜索)。

    http://blog.sqlauthority.com/2008/09/05/sql-server-creating-full-text-catalog-and-index/

    http://msdn.microsoft.com/en-us/library/ms142571.aspx

    【讨论】:

      猜你喜欢
      • 2012-05-04
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      相关资源
      最近更新 更多