Codeigniter join query multiple conditions don't work

廉价感情. 提交于 2020-08-19 07:47:53

问题


I want to select data from my database table with join query, but my it doesn't work.

My query:

$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->join('schedule', 'schedule.itemtype = 'testitem'');
$this->db->where('we.isActive','Y');

This line makes problem with schedule.itemtype = 'testitem':

 $this->db->join('schedule', 'schedule.itemtype = 'testitem'');

How can I solve this?


回答1:


You don't need to join same table twice. But just to extend ON clause:

$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid AND schedule.itemtype = \'testitem\'');
$this->db->where('we.isActive','Y');



回答2:


try

$this->db->select();
$this->db->from("we");
$this->db->join("schedule", "schedule.itemid = we.cid");
$this->db->where("schedule.itemtype","testitem");
$this->db->where("we.isActive","Y");



回答3:


I believe there are two problems here. The first problem is that you are using one too many quotes in the second join line in your query:

You have: $this->db->join('schedule', 'schedule.itemtype='testitem''); < extra quote

It should be: $this->db->join('schedule', 'schedule.itemtype=testitem');


Second problem: your join doesnt make sense.

Your statement:

$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->join('schedule', 'schedule.itemtype = testitem');
$this->db->where('we.isActive','Y');  

Translates to:

SELECT * FROM we
JOIN schedule ON schedule.itemid = we.cid
JOIN schedule ON schedule.itemtype = testitem
WHERE we.isActive = Y

As you can see you are joining the same table twice on different lines, not only that but what table does "testitem" belong to? We are left to assume that you perhaps want the join where itemtype = testitem which will mean this:

SELECT * FROM we
JOIN schedule ON schedule.itemid = we.cid
WHERE schedule.itemtype = testitem 
AND we.isActive = Y

Therefore your final Codeigniter query should be:

$this->db->select('*');
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->where('schedule.itemtype', 'testitem');
$this->db->where('we.isActive','Y'); 



回答4:


This will work:

$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->where('we.isActive','Y');
$this->db->where('schedule.itemtype', 'testitem');
$this->db->get('we');



回答5:


$this->db->query('select we_tbl.c_name from we we_tbl,schedule sch_tbl where sch_tbl.itemid = we_tbl.cid AND we_tbl.idActive = '.$activeData);

Try this query according to your problem this could get the data you need. I've tested on different database but i tried to perform what you're trying to get. https://www.w3schools.com/sql/trysql.asp?filename=trysql_op_in

select 
pro_tbl.ProductName, 
cat_tbl.CategoryName ,
sup_tbl.SupplierName 
from 
Products pro_tbl, 
Suppliers sup_tbl,
Categories cat_tbl 
where  
pro_tbl.SupplierID = sup_tbl.SupplierID AND
pro_tbl.CategoryID = cat_tbl.CategoryID;



回答6:


Two possible problems, depending on what your desired outcome is:

If you need to make two joins and are getting an error with the second join clause, try using double quotes to enclose the constant value on the condition or you'll get a parse error:

$this->db->join('schedule', 'schedule.itemtype = "testitem"');

If you need to join only once with multiple conditions, use parentheses:

$this->db->select('*');
$this->db->from('we');
$this->db->join('schedule', '(schedule.itemid = we.cid AND schedule.itemtype="testitem")');
$this->db->where('we.isActive','Y');

You query is equivalent to writing:

select * from we
inner join schedule on schedule.itemid = we.cid
inner join schedule on schedule.itemtype = "testitem"
where we.isActive = 'Y'

but what you seem to need is

select * from we
inner join schedule on (schedule.itemid = we.cid AND schedule.itemtype = "testitem")
where we.isActive = 'Y'

On your original query, you are doing two joins. In the latter, you'll do only one with multiple conditions.



来源:https://stackoverflow.com/questions/28995465/codeigniter-join-query-multiple-conditions-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!