我一直在用随机字符串测试我的查询,以查看是否会显示结果。
我测试的一个字符串是 cal
,因此查询应该很简单,对吧?
SELECT c.`ID` as id, CONCAT(p.`Name`, ' > ', c.`Name`) as name, 'cal' as q
FROM `cq_provice` p JOIN `cq_city` c ON c.`provinceID` = p.`ID`
WHERE (CONCAT(p.`Name`, ' > ', c.`Name`) LIKE '%cal%') ORDER BY p.`Name`, c.`Name`
LIMIT 0, 50
然而,我准备了2个sql查询:一个使用codeigniter方式,一个使用通常的:
#1
$this->db->select('c.ID as id');
$this->db->select('CONCAT(p.Name, \' > \', c.Name) as name');
$this->db->select($term.' as q');
$this->db->from('cq_provice p');
$this->db->join('cq_city c', 'c.provinceID = p.ID');
$this->db->like('CONCAT(p.Name, \' > \', c.Name)', $this->db->escape($this->input->post('search')), 'both');
$this->db->order_by('p.Name', 'ASC');
$this->db->order_by('c.Name', 'ASC');
$this->db->limit($result_count, $offset);
$query = $this->db->get();
#2
$query = "SELECT c.`ID` as id, CONCAT(p.`Name`, ' > ', c.`Name`) as name, ".$term." as q
FROM `cq_provice` p JOIN `cq_city` c ON c.`provinceID` = p.`ID`
WHERE (CONCAT(p.`Name`, ' > ', c.`Name`) LIKE
'%".$this->db->escape($this->input->post('search'))."%')
ORDER BY p.`Name`, c.`Name` LIMIT ".$offset.", ".$result_count;
他们两个都给了我相同的查询声明:
SELECT c.`ID` as id, CONCAT(p.`Name`, ' > ', c.`Name`) as name, 'cal' as q
FROM `cq_provice` p JOIN `cq_city` c ON c.`provinceID` = p.`ID`
WHERE (CONCAT(p.`Name`, ' > ', c.`Name`) LIKE '%'cal'%')
ORDER BY p.`Name`, c.`Name` LIMIT 0, 50
我的问题在于WHERE语句WHERE (CONCAT(p.
名称, ' > ', c.
名称) LIKE '%'cal'%'
其中LIKE语句应该是'%cal%'
所以我尝试使用trim和regex但是他们给了我同样的结果:
SELECT c.`ID` as id, CONCAT(p.`Name`, ' > ', c.`Name`) as name, 'cal' as q
FROM `cq_provice` p JOIN `cq_city` c ON c.`provinceID` = p.`ID`
WHERE (CONCAT(p.`Name`, ' > ', c.`Name`) LIKE 'Êl%')
ORDER BY p.`Name`, c.`Name` LIMIT 0, 50
我该如何解决这个问题?如何防止我的查询将我的字符串翻译为特殊字符?
非常感谢任何帮助。
答案 0 :(得分:0)
$this->db->select(array('c.ID as id','CONCAT(p.Name, " > ", c.Name) as name',$term.' as q'));
$this->db->from('cq_provice p');
$this->db->join('cq_city c', 'c.provinceID = p.ID');
$this->db->like('CONCAT(p.Name, " > ", c.Name)', $this->db->escape($this->input->post('search')));
$this->db->order_by('p.Name ASC, c.Name ASC');
$this->db->limit($result_count, $offset);
$query = $this->db->get();
或MATCH
,AGAINST
$field = $this->db->escape($this->input->post('search'));
$this->db->select(array('c.ID as id','CONCAT(p.Name, " > ", c.Name) as name',$term.' as q'));
$this->db->from('cq_provice p');
$this->db->join('cq_city c', 'c.provinceID = p.ID');
$this->db->where('MATCH (p.Name,c.Name) AGAINST ('.$field.')', NULL, FALSE);
$this->db->order_by('p.Name ASC, c.Name ASC');
$this->db->limit($result_count, $offset);
$query = $this->db->get();