我有两个不同的表
作为testimonial
和pagetitles
。从 database
获取 Pagetitles
时。我需要比较前 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_id | pagetitle |
+--------------+-------------+
| 1 | testimonial |
| 2 | career |
+--------------+-------------+
最佳答案
我认为你必须使用 if then statement在 MySQL 中, 所以如果我们想更改您的查询,它必须是这样的:
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
)
我没有测试过,但我认为这一定是真的
关于php - 比较表中的前 4 个字符并从 codeigniter 中的数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993309/