【问题标题】:Can I use two Where in select statement我可以在 select 语句中使用两个 Where 吗
【发布时间】:2013-04-13 01:42:04
【问题描述】:

我正在尝试在查询中获取日期范围和交易类型。我正在使用 Mysql 数据库在 PHP 中工作。 原来,声明是

         ("select * from accounting WHERE date between '$startdate' AND 
        '$enddate'",$prop);

它工作得很好,没有问题@all。但我还需要它来选择数据库中称为事务类型的列。 这是我修改它的方法:

           ("SELECT date, account, description, Income FROM accounting where 
           transactiontype = 'Income' and WHERE date between '$startdate' AND 
           '$enddate' order by date ASC",$prop);

我收到错误消息: 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“WHERE date between '2012/09/01' AND '2013/04/17' order by date ASC”附近使用正确的语法 查询:SELECT date, account, description, Income FROM accounting where transactiontype = 'Income' and WHERE date between '2012/09/01' AND '2013/04/17' order by date ASC 任何帮助将不胜感激。

【问题讨论】:

  • 把第二个放在哪里。 (PS:我没有投反对票)。

标签: php mysql select where


【解决方案1】:

AND 就足够了,你不需要第二个 WHERE。

("SELECT `date`, account, description, Income 
  FROM accounting 
  WHERE transactiontype = 'Income' 
  AND `date` between '$startdate' 
  AND '$enddate' order by `date` ASC",$prop);

顺便说一句,尽量避免保留关键字作为表/列名称(如日期)。 您将不得不一直逃避它们(并且会一直忘记这一点)。

【讨论】:

  • 有效!感谢您的及时帮助!
【解决方案2】:

请删除第二个 where 子句..

       (SELECT date, account, description, Income FROM accounting where 
       transactiontype = 'Income' and (date between '$startdate' AND 
       '$enddate') order by date ASC",$prop )

【讨论】:

    【解决方案3】:
    ("SELECT date, account, description, Income FROM accounting where 
      transactiontype = 'Income' and "date" between '$startdate' AND 
     '$enddate' order by date ASC",$prop);
    

    您可能还需要将日期括在语音标记或勾号 (`) 中,如图所示。

    【讨论】:

    • 是的 - 我会向你的知识低头! ;o)
    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 2017-05-07
    • 2015-09-16
    • 1970-01-01
    • 2012-04-28
    • 2010-11-05
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多