在另一个字符串中的某个单词之前插入字符串

时间:2014-10-25 12:01:33

标签: php mysql

我合并了我的MySQL查询,因此用户可以选择多个下拉列表并根据这些下拉列表进行过滤。这是按照我使用$ echo查询检查的预期工作。但是,我需要插入" AND"在每次出现之前#34; WHERE"除了第一个。如果我选择多个drodowns,我的查询就会出现这样:

SELECT a.MembershipID,
       a.FirstName,
       a.Surname,
       t.RaceID,
       t.Time,
       r.RaceID,
       r.RaceName,
       r.Distance,
       r.ClubYear
FROM Athlete AS a
INNER JOIN Time AS t ON a.MembershipID=t.MembershipID
INNER JOIN Race AS r ON t.RaceID=r.RaceID
WHERE r.RaceName= 'Parkrun' WHERE r.Distance= '5 kilometres'
ORDER BY a.Surname

正如您在第4行所看到的,有两个WHERE查询没有AND。如何在第二个WHERE之前插入AND?除了第一个以外,我怎样才能为每个后续的WHERE做这个?我想过使用explode创建子串,插入AND然后将它们连接在一起,但这似乎有点过头了。有帮助吗? 感谢。

编辑:添加生成上述内容的PHP代码:

$query = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID";
            if ($_POST['raceName']!= null) {
                $raceName = mysqli_real_escape_string($mysqli, $_POST["raceName"]);
                $filter = " WHERE r.RaceName= '$raceName'";
                $query .= $filter;
            } 
            if ($_POST['athlete']!= null) {
                $athlete = mysqli_real_escape_string($mysqli, $_POST["athlete"]);
                $firstWord = explode(' ',trim($athlete));
                $firstName = $firstWord[0];
                $lastWord = explode(' ', $athlete);
                $surname = array_pop($lastWord);
                $filter = " WHERE a.FirstName= '$firstName' AND a.Surname = '$surname'";
                $query .= $filter;
            } 
            if ($_POST['distance']!= null) {
                $distance = mysqli_real_escape_string($mysqli, $_POST["distance"]);
                $filter = " WHERE r.Distance= '$distance'";
                $query .= $filter;
            } 
            if ($_POST['clubYear']!= null) {
                $clubYear = mysqli_real_escape_string($mysqli, $_POST["clubYear"]);
                $filter = " WHERE r.ClubYear= '$clubYear'";
                $query .= $filter;
            }

            $fullquery = $query . " ORDER BY a.Surname";

2 个答案:

答案 0 :(得分:1)

一个简单的解决方案是首先添加一个始终真实的语句,如1=1,这样您就不必检查它是否是第一个语句,然后通过附加AND CONDITION_N继续构建查询相应

$query = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID";

$query .= " WHERE 1 = 1 ";
if ($_POST['raceName']!= null) {
    $raceName = mysqli_real_escape_string($mysqli, $_POST["raceName"]);
    $filter = " AND r.RaceName= '$raceName'";
    $query .= $filter;
} 

说明:

  1. 如果没有或您的if条件属实,那么您的查询将是您的初始查询所遵循的WHERE 1=1,这与首先没有它的情况完全相同
  2. 如果您的任何条件为true,那么它将是:

      

    INITIAL_QUERY WHERE 1=1 AND CONDITION_N

  3. 对于多个条件为真,它将是:

      

    INITIAL_QUERY WHERE 1=1 AND CONDITION_N AND CONDITION_N+1

  4. 这使得PHP代码更清晰,更快,因为您不必检查它是否是第一个条件,以便在附加到select语句时决定是使用WHERE还是AND

答案 1 :(得分:0)

也许你可以做类似

的事情
WHERE id > 0

之前的

WHERE r.RaceName= 'Parkrun' WHERE r.Distance= '5 kilometres'

你会有像

这样的东西
WHERE id > 0 AND r.RaceName= 'Parkrun' AND r.Distance= '5 kilometres'