[英]Comparing first 4 characters from table and fetching data from database in codeigniter
I have two different tables
as testimonial
and pagetitles
. 我有两个不同的
tables
作为testimonial
和pagetitles
。 While fetching the Pagetitles
from database
. 从
database
提取Pagetitles
时。 I need to compare first 4 characters if both matches then I should get the data. 我需要比较前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;
}
}
Database tables 数据库表
testimonials: 推荐:
+----------------+-------------------+-------------+
| testimonial_id | testimonial_name | client_name |
+----------------+-------------------+-------------+
| 1 | testimonial | abc |
| 2 | testimonial | def |
+----------------+-------------------+-------------+
Pagetitle 页面标题
+--------------+-------------+
| pagetitle_id | pagetitle |
+--------------+-------------+
| 1 | testimonial |
| 2 | career |
+--------------+-------------+
i think you have to use if then statement in mysql , so if we want to change your query , it must be something like this : 我认为你必须在mysql中使用if then语句 ,所以如果我们想要更改你的查询,它必须是这样的:
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
)
i dont test it , but i think it must be true 我不测试它,但我认为它一定是真的
If you do not got any solutions yet try this below code, 如果您还没有任何解决方案,请尝试以下代码,
$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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.