【问题标题】:Comparing first 4 characters from table and fetching data from database in codeigniter在codeigniter中比较表中的前4个字符并从数据库中获取数据
【发布时间】:2017-05-16 05:41:05
【问题描述】:

我有两个不同的tables,分别是testimonialpagetitles。从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      |
+--------------+-------------+

【问题讨论】:

  • @Sinto 在这里他们使用 like 运算符如果 testimonial_name 是 testimonial 而 page_title 是 testimonials 那么它将不匹配那么我们如何为此目的获取数据我只需要比较前 4 个字符跨度>
  • 尝试使用 SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4)你的加入。
  • @Sinto 你能在我的代码中编辑吗
  • 有用吗?

标签: php mysql codeigniter


【解决方案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
)

我没有测试它,但我认为它一定是真的

【讨论】:

  • 不理解查询
  • @user8001297 这个查询告诉 mysql,直到 SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4) 为假,不要从 where title >5 中选择,所以如果语句是真的,你的查询将是午餐
【解决方案2】:

如果您还没有任何解决方案,请尝试以下代码,

$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;
}

【讨论】:

  • 在 where 子句中将错误作为未知列推荐
  • 我们的查询中有“推荐”吗?您指定的“数据库表”中也没有这样的列
  • 推荐是我的页面标题,没有这样的推荐栏
  • $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 = '{$id}'" )->result_array(); //print_r($testimonials); if($testimonials->num_rows()>0) { $output = $testimonials->result();返回$输出[0]; } 否则 { 返回假; }
  • 那里你不需要检查num_rowscount($testimonials) & $output = $restimonials
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
相关资源
最近更新 更多