选择查询不工作,因为它应该工作- MYSQL


Select Query is not working as it should work - MYSQL

我的数据库中有这样的数据代表姓名,状态和案件类型律师:

name: new attar

状态: a:2:{i:0;s:13:"Massachusetts";i:1;s:13:"New Hampshire";}

案例类型 a:13:{i:0;s:1:"5";i:1;s:1:"7";i:2;s:1:"8";i:3;s:1:"9";i:4;s:2:"10";i:5;s:2:"11";i:6;s:2:"13";i:7;s:2:"14";i:8;s:2:"15";i:9;s:2:"16";i:10;s:2:"17";i:11;s:2:"18";i:12;s:2:"19";}

name: Kevin Regan

状态: a:1:{i:0;s:13:"Massachusetts";}

案例类型 a:1:{i:0;s:2:"14";}

name: Matthew Gendreau

状态: a:2:{i:0;s:13:"Massachusetts";i:1;s:13:"New Hampshire";}

案例类型 a:16:{i:0;s:15:"multiselect-all";i:1;s:1:"5";i:2;s:1:"2";i:3;s:1:"7";i:4;s:1:"8";i:5;s:1:"9";i:6;s:2:"10";i:7;s:2:"11";i:8;s:1:"1";i:9;s:2:"13";i:10;s:2:"14";i:11;s:2:"15";i:12;s:2:"16";i:13;s:2:"17";i:14;s:2:"18";i:15;s:2:"19";}

我使用的查询是:

SELECT
    *
FROM
    `web_attorney`
WHERE
    (
        `at_status` = '1'
        OR (
            `at_status` = '2'
            AND '2015-05-15' NOT BETWEEN `at_inactive_date_from`
            AND `at_inactive_date_to`
            AND `at_inactive_date_from` != ''
            AND `at_inactive_date_to` != ''
        )
    )
AND (
    `at_juris_states` LIKE '%Massachusetts%'
)
AND (
    `at_availability` LIKE '%Friday%'
)
AND (`at_cases` LIKE '%2%')
ORDER BY
    `at_has_mt` ASC
LIMIT 5

问题:

我想获取记录只在cases type like 2

因为我在PHP上使用序列化数组,所以查询确实有效,但问题是它总是出现在cases types not 2 and 2。两者都是通过这个问题来的。

它还选择拥有not case type 2的律师。像new attar在双引号"2"中没有case type 2,但在我的查询中,当他没有时,他出现了。

下面是小提琴的例子:

http://sqlfiddle.com/!9/e4c3c/1

标准化的方法可能看起来像这样:

attorneys
attorney_id*, name
          2 , Kevin Regan
          3 , Matthew  Gendreau

attorney_state
attorney_id*, state*
          2 , Massachusetts
          3 , Massachusetts
          3 , New Hampshire
attorney_casetype
attorney_id,casetype
          2,      14
          3,       2 
          3,       7
          3,       8
          3,       9
          3,      10
          3,      11
          3,       1
          3,      13
          3,      14
          3,      15
          3,      16
          3,      17
          3,      18
          3,      19         

* = (component of) PRIMARY KEY