基本上我的愿望是从条件到序列化数据列(ans)的行中计算行数。 并获得存储在'ans'col。
中的每个选择的尝试选择计数如果回答类型是无线电然后回答(在反序列化时的ans col下)将是int,如果回答逗号分隔的字符串然后它是复选框否则它将是评论答案意味着将是文本。
Database table Schema(test11_answer):
+-------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| survey_id | int(10) unsigned | NO | | NULL | |
| question_id | int(10) unsigned | NO | | NULL | |
| user_id | int(10) unsigned | NO | | NULL | |
| ans | text | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------+------------------+------+-----+---------------------+----------------+
Data in Table(test11_answer):
+----+-----------+-------------+---------+-------------------------------------------------------------------+---------------------+---------------------+
| id | survey_id | question_id | user_id | ans | created_at | updated_at |
+----+-----------+-------------+---------+-------------------------------------------------------------------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | a:2:{s:8:"answered";s:1:"1";s:4:"type";s:5:"radio";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
| 2 | 1 | 2 | 1 | a:2:{s:8:"answered";s:3:"5,6";s:4:"type";s:3:"box";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
| 3 | 1 | 3 | 1 | a:2:{s:8:"answered";s:15:"kasdkfjasldkfj";s:4:"type";s:4:"text";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
| 4 | 1 | 3 | 1 | a:2:{s:8:"answered";s:15:"kasdkfjasldkfj";s:4:"type";s:4:"text";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
| 5 | 1 | 1 | 1 | a:2:{s:8:"answered";s:1:"1";s:4:"type";s:5:"radio";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
| 6 | 1 | 2 | 1 | a:2:{s:8:"answered";s:3:"5,6";s:4:"type";s:3:"box";} | 2015-02-09 08:10:23 | 2015-02-09 08:10:23 |
+----+-----------+-------------+---------+-------------------------------------------------------------------+---------------------+---------------------+
所以首先,如果我想检查选择的数量(通过选择ID)和类型是无线电,原始的mysql查询即时使用:
SELECT COUNT(*) AS total FROM `test11_answer`
WHERE `question_id` = 1 AND `survey_id` = 1 AND
TRIM(BOTH '\"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(`ans`,';',2),':',-1)) = 1 AND
TRIM(BOTH '\"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(`ans`,';',4),':',-1)) LIKE '%radio%';
+-------+
| total |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
但是当我的情况下此查询转换为laravel eloquent模型时:
Answer::where('question_id', '=', 1)
->where('survey_id', '=', 1)
->whereRaw('TRIM(BOTH \'"\' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,\';\',2),\':\',-1)) IN (?)', array(1))
->whereRaw('TRIM(BOTH \'"\' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,\';\',4),\':\',-1)) LIKE (\'%?%\')', array("radio"))
->count();
顺便说一句,答案就是Eloquent Model,它会返回(0)。 最终生成的查询(在字符串中)在mysql中正常工作,但此函数始终返回0。
更新 生成的查询对象是:(根据laravel)
array (size=3)
'query' => string 'select count(*) as aggregate from `test11_answer` where `question_id` = ? and `survey_id` = ? and TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,';',2),':',-1)) IN (?) and TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,';',4),':',-1)) LIKE ('%?%')' (length=263)
'bindings' =>
array (size=4)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string 'radio' (length=5)
'time' => float 0.28
最终的查询将是(进入数据库)
select count(*) as aggregate from `test11_answer`
where `question_id` = 1 and `survey_id` = 1 and
TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,';',2),':',-1)) IN (1) and
TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(ans,';',4),':',-1)) LIKE ('%radio%')
此生成的查询正常工作,并在mysql中提供正确的结果。
在我看来,它看起来像方法中的一些错误但很少混淆它真的与否。
同样,我需要计算'text'和'box'类型
如果仍然不清楚,请告诉我,我会尝试进一步详细解释/更新我的问题。