我有两个不同的tables
testimonial
和pagetitles
。从Pagetitles
获取database
时。我需要比较前4个字符,如果两个匹配,那么我应该得到数据。
function getpagetitle($id)
{
$this->db->select('P.*,T.testimonial_name');
$this->db->from('pagetitle AS P');
$this->db->join('testimonials AS T','SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4)','INNER');
$this->db->where(array('P.page_title'=>$id));
$q=$this->db->get();
//var_dump($this->db->last_query());
//print_r($q->num_rows());
if($q->num_rows()>0)
{
$output = $q->result();
return $output[0];
}
else
{
return false;
}
}
数据库表
推荐:
+----------------+-------------------+-------------+
| testimonial_id | testimonial_name | client_name |
+----------------+-------------------+-------------+
| 1 | testimonial | abc |
| 2 | testimonial | def |
+----------------+-------------------+-------------+
PAGETITLE
+--------------+-------------+
| pagetitle_id | pagetitle |
+--------------+-------------+
| 1 | testimonial |
| 2 | career |
+--------------+-------------+
答案 0 :(得分:1)
我认为你必须在mysql中使用if then statement, 因此,如果我们想要更改您的查询,它必须是这样的:
select P.*,T.testimonial_name from pagetitle AS P , testimonials as T (
select IF 'SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4)'
where P.page_title>5
)
我不测试它,但我认为一定是真的
答案 1 :(得分:1)
如果您还没有任何解决方案,请尝试以下代码,
$testimonials = $this->db->query("SELECT `P`.*, `T`.`testimonial_name`
FROM (`pagetitle` P) INNER JOIN `testimonials` T ON
((SUBSTR(`T`.`testimonial_name`, 1, 4)) = (SUBSTR(`P`.`page_title`, 1, 4)))
WHERE `P`.`page_title` = SUBSTR('{$id}', 1, 4)")->result_array();
print_r($testimonials);
/* Output of $testimonials will be like below:
Array
(
[0] => Array
(
[fieldname_1] => val
[fieldname_2] => val
)
[1] => Array
(
[fieldname_1] => val
[fieldname_2] => val
)
)
*/
if(count($testimonials) > 0) {
//$output = $testimonials->result();
return $testimonials[0];
} else {
return false;
}