使用like和Left Join

时间:2015-12-03 13:39:51

标签: php mysql join

我正在处理一个搜索查询,它会获取一个术语并显示结果。我已经弄明白了大部分内容。但是我的问题出现在查询中,它假设要搜索类别并将其提升,但它也会假设在类别中调出产品。

查询

$query = "SELECT brands.braname, brands.braimg, brands.braid, Products.proid, Products.name FROM brands
LEFT JOIN Products ON brands.braid = Products.proid
WHERE braname LIKE $search ORDER BY braid ASC LIMIT 1";

4 个答案:

答案 0 :(得分:0)

LEFT JOIN Products ON brands.braid = Products.proid “产品”有编织吗? 也许它将是ON LEFT JOIN Products ON Products.braid = brands.braid

答案 1 :(得分:0)

尝试在搜索项上添加一些通配符,例如:

    angular.module('myapp')
      .config(function ($stateProvider) {
        $stateProvider
          .state('login', {
            url: '/',
            templateUrl: 'app/account/login/login.html',
            controller: 'LoginCtrl'
          })
          .state('signup', {
            url: '/signup',
            templateUrl: 'app/account/signup/signup.html',
            controller: 'SignupCtrl'
          })
          .state('settings', {
            url: '/settings',
            templateUrl: 'app/account/settings/settings.html',
            controller: 'SettingsCtrl',
            authenticate: true
          });
      });

答案 2 :(得分:0)

LIKE需要一个字符串作为输入,因此您需要单引号。像这样:

$query = "SELECT brands.braname, brands.braimg, brands.braid, Products.proid, Products.name 
FROM brands LEFT JOIN Products 
ON brands.braid = Products.proid 
WHERE braname LIKE '".$search."' ORDER BY braid ASC LIMIT 1";

此外,如果你没有使用任何通配符,你也可以停止混淆自己并使用'='代替'LIKE':

$query = "SELECT brands.braname, brands.braimg, brands.braid, Products.proid, Products.name 
FROM brands LEFT JOIN Products 
ON brands.braid = Products.proid 
WHERE braname = '".$search."' ORDER BY braid ASC LIMIT 1";

如果您需要检查列分支是否包含$ search ,则需要使用'%'通配符代表任意数量的符号:< / p>

$query = "SELECT brands.braname, brands.braimg, brands.braid, Products.proid, Products.name 
FROM brands LEFT JOIN Products 
ON brands.braid = Products.proid 
WHERE braname LIKE '%".$search."%' ORDER BY braid ASC LIMIT 1";

答案 3 :(得分:0)

试试这个

$query = "SELECT brands.braname, brands.braimg, brands.braid, Products.proid, Products.name
          FROM brands
          LEFT JOIN Products ON brands.braid = Products.proid 
          WHERE braname LIKE %$search% 
          ORDER BY braid ASC 
          LIMIT 1";

您需要添加围绕搜索字词的百分比符号。