雄辩的原始查询MySQL JSON

时间:2018-07-13 05:34:37

标签: mysql json laravel-5 eloquent

我试图找出正确的语法,将AND运算符添加到以下Eloquent Raw查询中,在该查询中,我正在查询MySQL(5.7.9)表的JSON字段。这样,我希望能够具有不区分大小写的功能。

在对如何实现这一目标进行了初步研究之后,我的代码以这种基本方式工作:

$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?)', ["%{$user_name}%"])

但是我的目标是添加一个 AND 运算符,以使用非JSON varchar列进一步缩小搜索范围。

我已经尝试过此操作(以及其他没有成功的变体):

$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?)', 'and user_type = admin', ["%{$user_name}%"])

这给我一个错误: “数组到字符串的转换”

我也尝试过:

$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?) and user_type = admin', ["%{$user_name}%"])

给我以下错误: “ SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'user_type'”

其他未导致错误的类似变体产生的结果太多或太少。

如何成功调整语法以查询具有正确结果的其他非JSON字段?另外,使用Eloquent,MySQL(5.79)和Laravel 5.6进行这样的原始查询是否是最有效的方法?

感谢您提供的所有帮助/指示! :)

2 个答案:

答案 0 :(得分:1)

我认为您应该做类似的事情

$users = User::where(DB::raw('lower(info_json->"$.full_name")', 'like', DB::raw('lower(%{$user_name}%)'))
->where('user_type', '=', 'admin')

可能我有一些sintax错误,但主要思想是使用DB::raw来帮助您进行查询。

答案 1 :(得分:0)

我知道了。我的语法应该看起来像这样:

{
  "posts": [
    {
      "comments_count": 0,
      "day": "2017-09-13",
      "id": 1,
      "name": "Awesome Idea #9",
      "product_state": "default",
      "tagline": "Great new search engine",
      "ios_featured_at": null,
      "category_id": null,
      "created_at": "2017-09-13T00:00:00.000-07:00",
      "current_user": {},
      "discussion_url": "http://www.producthunt.com/posts/awesome-idea-9?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Awesome+Oauth+App+%2310+%28ID%3A+1%29",
      "exclusive": null,
      "featured": true,
      "maker_inside": false,
      "makers": [],
      "platforms": [],
      "redirect_url": "http://www.producthunt.com/r/15a7ee196d4213/1?app_id=1",
      "screenshot_url": {
        "300px": "http://placehold.it/850x850.png",
        "850px": "http://placehold.it/850x850.png"
      },
      "thumbnail": {
        "id": 1,
        "media_type": "image",
        "image_url": "https://ph-files.imgix.net/abe23316-977b-4428-aa3f-00b7d24b2d36?auto=format\u0026fit=crop\u0026h=570\u0026w=430",
        "metadata": {}
      },
      "topics": [],
      "user": {
        "id": 1,
        "created_at": "2017-09-14T06:01:54.757-07:00",
        "name": "Karl User the 44th",
        "username": "producthunter44",
        "headline": "Product Hunter",
        "twitter_username": "hunter44",
        "website_url": "http://awesome.io",
        "profile_url": "http://www.producthunt.com/@producthunter44",
        "image_url": {
          "30px": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
          "original": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4.png"
        }
      },
      "votes_count": 0
    },

      "user": {
        "id": 7,
        "created_at": "2017-09-14T06:01:54.931-07:00",
        "name": "Karl User the 50th",
        "username": "producthunter50",
        "headline": "Product Hunter",
        "twitter_username": "hunter50",
        "website_url": "http://awesome.io",
        "profile_url": "http://www.producthunt.com/@producthunter50",
        "image_url": {

          "88px@3X": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4.png",
          "original": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4.png"
        }

感谢那些协助过的人!像许多语法/编码问题一样,稍作休息可以帮助我在返回时很快地弄清楚它。