How to wirte a left join query in codeigniter? - mysql

can some one tell me how to write this mysql query in codeigniter??
SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

Try this
$this->db->select(*);
$this->db->from('t1');
$this->db->join('t2','t2.a = t1.a','left');
$this->db->join('t3','t3.b = t1.b','left');
$this->db->join('t4','t4.c = t1.c','left');
Alternatively (condensed)
$this->db->select(*)->from('t1')->join('t2','t2.a = t1.a','left')->join('t3','t3.b = t1.b','left')->join('t4','t4.c = t1.c','left');
Reference: http://ellislab.com/codeigniter/user-guide/database/active_record.html

Related

Laravel Eloquent - 2 seperate table with own left join

I have the following 4 tables
aa:id, xx_id, bb_id
bb:id
xx:id, zz_id
zz.id
I would like to do a query like this
SELECT *
FROM
aa LEFT JOIN bb ON (aa.bb_id = bb.id),
xx LEFT JOIN zz ON (xx.zz_id = zz.id)
WHERE aa.xx_id = xx.id
How do I achieve this through Laravel Eloquent?
Since using a comma to separate tables in the FROM clause is the same as using a CROSS JOIN, you should first rewrite your query to use a CROSS JOIN.
SELECT *
FROM
(SELECT * FROM aa LEFT JOIN bb ON aa.bb_id = bb.id) T1
CROSS JOIN
(SELECT * FROM xx LEFT JOIN zz ON xx.zz_id = zz.id) T2
WHERE
aa.xx_id = xx.id
// First define the subqueries.
$t1 = DB::table('aa')->leftJoin('bb', 'aa.bb_id', 'bb.id');
$t2 = DB::table('xx')->leftJoin('zz', 'xx.zz_id', 'zz.id');
// Do the query.
$results = DB::table($t1, 'T1')
->crossJoinSub($t2, 'T2')
->whereColumn('aa.x_id', 'xx.id')
->get();
Alternatively, you can pass the whole FROM clause to the fromRaw method.
$results = DB::query()
->fromRaw('aa LEFT JOIN bb ON (aa.bb_id = bb.id), xx LEFT JOIN zz ON (xx.zz_id = zz.id)')
->whereColumn('aa.x_id', 'xx.id')
->get();

MySQL LEFT JOIN example with conditions

I am using following query to retrieve the needed objects:
SELECT *
FROM tb_po_items
LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material
LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service
WHERE po_id =47
Now I need to add following:
Condition 1 -> if tb_po_items.mos = 1 then LEFT JOIN tb_units ON tb_materials.material_unit = tb_units.id_unit
else
Condition 2 -> if tb_po_items.mos = 2 then LEFT JOIN tb_units ON tb_services.service_unit = tb_units.id_unit
How can I implement the use of both conditions in the MySQL query?
Thanks in advance.
Try this:
SELECT *
FROM tb_po_items
LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material
LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service
LEFT JOIN tb_units ON (
(tb_materials.material_unit = tb_units.id_unit AND tb_po_items.mos = 1)
OR
(tb_services.service_unit = tb_units.id_unit AND tb_po_items.mos = 2) )
WHERE po_id =47;
hi this is experimental so check before you go live.
SELECT * FROM tb_po_items
IF(tb_po_items.mos = 1, LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material, LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service)
WHERE po_id =47

Inner Join Nested Inside Update Statement SQL

I am trying to write a query that performs an inner join within an update statement.
Here is the current query I am working with:
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable`
SET
visual = t2.visual,
inspection_status = t2.inspection_status,
inspector_name = t2.inspector_name
FROM
singulation1.`q096_cq33r08-ds01-n-testtable` t1
INNER JOIN
singulation1.`q014_bq31t05-dw30-x` t2
ON
(t1.print = t2.print AND t1.id3 = t2.id3);
Something about MySql does not like the FROM clause.
For updates, you specify the join in the update clause
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable` AS t1
INNER JOIN singulation1.`q014_bq31t05-dw30-x` AS t2
ON t1.print = t2.print AND t1.id3 = t2.id3
SET
t1.visual = t2.visual
t1.inspection_status = t2.inspection_status,
t1.inspector_name = t2.inspector_name

Mysql update using values from select

I have two queries. The first will return multiple rows:
SELECT parent_entry_id,child_entry_id FROM exp_playa_relationships WHERE parent_field_id = '34';
...And I would like to use the values (parent_entry_id,child_entry_id) and incorporate them into this query, replacing 'x' and 'y', and do it for each row returned by the first query.
UPDATE exp_channel_data AS t1,
(
SELECT field_id_46,field_id_47 FROM exp_channel_data WHERE entry_id = 'x') AS t2
SET t1.field_id_60 = t2.field_id_46, t1.field_id_61 = t2.field_id_47
WHERE t1.entry_id = 'y';
I think I need to use another JOIN, but I can't figure out how to implement one in my example. Any help would be much appreciated.
I think this is what you're after:
UPDATE exp_playa_relationships AS t0
JOIN exp_channel_data AS t1
ON t1.entry_id = t0.child_entry_id
JOIN exp_channel_data AS t2
ON t2.entry_id = t0.parent_entry_id
SET t1.field_id_60 = t2.field_id_46
, t1.field_id_61 = t2.field_id_47
Try this query
UPDATE exp_channel_data a1 INNER JOIN exp_playa_relationships a ON a1.entry_id = a.child_entry_id
INNER JOIN exp_channel_data b ON a.parent_entry_id = b.entri_id
SET a1.field_id_60 = b.field_id_46, ta1.field_id_61 = b.field_id_47
WHERE parent_field_id = '34'
Thanks all for your replies. The working syntax is:
UPDATE exp_channel_data AS t1,
(
SELECT
entry_id as ei2, child_entry_id, parent_entry_id, field_id_46 as f46,field_id_47 as f47
FROM
exp_channel_data JOIN exp_playa_relationships ON entry_id=child_entry_id AND parent_field_id = 34) AS t2
SET t1.field_id_60 = f46, t1.field_id_61 = f47
WHERE t1.entry_id=parent_entry_id;
Or in a more classic syntax, you need to adjust to your own foo & bar attributes, but use something like the following:
update exp_channel_data t1
set (t1.field_id_60,t1.field_id_61) = (
select t2.field_id_46 , t2.field_id_47
from exp_channel_data t2
where 1=1
and t2.entry_id = 'x'
and /* ENTER YOUR t1-t2 join condition here */
)
where 1=1
and t1.entry_id = y
;
But since you are MySQL I don't believe it supports compound subquery. As such:
update exp_channel_data t1
set t1.field_id_60 = (
select t2.field_id_46
from exp_channel_data t2
where 1=1
and t2.entry_id = 'x'
and /* ENTER YOUR t1-t2 join condition here */
) , t1.field_id_61 = (
select t3.field_id_47
from exp_channel_data t3
where 1=1
and t3.entry_id = 'x'
and /* ENTER YOUR t1-t3 join condition here */
)
where 1=1
and t1.entry_id = y
;

Linq to SQL with more conditiones on one join

I'm new to Linq to SQL and I'm trying to transform this SQL into Linq. Could you please help me out.
SELECT *
FROM [dbo].[tblTest]
INNER JOIN [dbo].[tblStationTest] ON [tblTest].[id] = [tblStationTest].[Test_id]
INNER JOIN [dbo].[tblTestType] ON [tblTest].[TestType_id] = [tblTestType].[id]
LEFT OUTER JOIN [dbo].[tblTestOrder] ON [tblTest].[id] = [tblTestOrder].[Test_id]
AND ([tblTestOrder].[TestOrderList_id] = 1)
WHERE ([Station_id] = 1)
What is causing me problem is this condition AND ([TestOrderList_id] = 1)
This condition can't be in Where clase because it will cancel the effect of Left Join
Thanks kurin
you can try something like
from test in db.tblTest
join stationTest in db.tblStationTest on test.id equals stationTest.Test_id
join testType in db.tblTestType on test.TestType_id equals testType.id
join testOrder in db.tblTestOrder on new{Key1 = test.id, Key2= 1} equals new{Key1 = testOrder.Test_id, Key2 = testOrder.TestOrderList_id} into tempOrders
from t in tempOrders.DefaultIfEmpty()
select new{test.Test_id, testType.something, t.somethingelse}