Hi! I am trying to figure out how to do a join in multiple steps in the same line of code. This is what i got so far, but I get an error 1064.
select * from jbcity
join jbsupplier on jbsupplier.city = jbcity.name where jbcity.state = "Mass"
join jbsupply on jbsupply.supplier = jbsupplier.id
join jbparts on jbparts.id = jbsupply.part;
Try this:
select * from jbcity
join jbsupplier on jbsupplier.city = jbcity.name
join jbsupply on jbsupply.supplier = jbsupplier.id
join jbparts on jbparts.id = jbsupply.part where jbcity.state = 'Mass';
Related
I am new in code igniter. I am trying to execute delete query using joining multiple tables, but I am getting error in query.
Here is my query code.
$this->db->from('order');
$this->db->join('item_order', 'item_order.order_id = order.order_id','left');
$this->db->join('product', 'product.product_number = item_order.item_number','left');
$this->db->join('product_to_image', 'product_to_image.p_id = product.products_id','left');
$this->db->join('product_to_dropbox', 'product_to_dropbox.products_id = product.products_id','left');
$this->db->where('order.user_name', $ebay_user_name);
$this->db->where('order.user_id', $user_id);
$this->db->delete('order');
When using Codeigniter you can not perform a DELETE with Join. see here and here.
You need to write a natural SQL. See link 1:
$sql = "DELETE o FROM order as o
LEFT JOIN item_order ON item_order.order_id = order.order_id
LEFT JOIN product ON product.product_number = item_order.item_number
LEFT JOIN product_to_image ON product_to_image.p_id = product.products_id
LEFT JOIN product_to_dropbox ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));
OR try with backticks `. But I'm not sure whether or not escaping them:
$sql = "DELETE o FROM `order` as o
LEFT JOIN `item_order` ON item_order.order_id = order.order_id
LEFT JOIN `product` ON product.product_number = item_order.item_number
LEFT JOIN `product_to_image` ON product_to_image.p_id = product.products_id
LEFT JOIN `product_to_dropbox` ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));
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
I get the error [Err] 1066 - Not unique table/alias: 'vtiger_leadscf'
UPDATE vtigerdb.vtiger_leadscf
JOIN vtiger_new.vtiger_leaddetails ON vtiger_new.vtiger_leadscf.leadid = vtiger_new.vtiger_leaddetails.leadid
JOIN vtiger_new.vtiger_leadaddress ON vtiger_new.vtiger_leadaddress.leadaddressid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_crmentity ON vtiger_new.vtiger_crmentity.crmid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_users ON vtiger_new.vtiger_users.id = vtiger_new.vtiger_crmentity.smcreatorid
JOIN vtigerdb.vtiger_leadscf ON vtigerdb.vtiger_leadscf.leadid = vtiger_new.vtiger_leadscf.leadid
SET vtigerdb.vtiger_leadscf.cf_953 = CONCAT(vtiger_new.vtiger_users.first_name,' ',vtiger_new.vtiger_users.last_name)
WHERE vtigerdb.vtiger_leadscf.leadid = vtiger_new.vtiger_leadscf.leadid
AND cf_953 = ""
can't figure out whats wrong with it.
Last join and update reference the same table.
Use JOIN table AS other_name to avoid this problem.
Here is the corrected query.
Plus I think you meant vtiger_new on the last join and not vtigerdb as you wrote.
UPDATE vtigerdb.vtiger_leadscf
JOIN vtiger_new.vtiger_leaddetails ON vtiger_new.vtiger_leadscf.leadid = vtiger_new.vtiger_leaddetails.leadid
JOIN vtiger_new.vtiger_leadaddress ON vtiger_new.vtiger_leadaddress.leadaddressid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_crmentity ON vtiger_new.vtiger_crmentity.crmid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_users ON vtiger_new.vtiger_users.id = vtiger_new.vtiger_crmentity.smcreatorid
JOIN vtiger_new.vtiger_leadscf AS vlead ON vtigerdb.vtiger_leadscf.leadid = vlead.leadid
SET vtigerdb.vtiger_leadscf.cf_953 = CONCAT(vtiger_new.vtiger_users.first_name,' ',vtiger_new.vtiger_users.last_name)
WHERE vtigerdb.vtiger_leadscf.leadid = vlead.leadid
AND cf_953 = ""
I have a select:
SELECT
oa.einheit,
op.id, op.artikelnummer,
op.preis_pro_einheit, op.p1_einheit, op.p2_einheit, op.p3_einheit, op.zusatztext, op.position,
oa.artikel, oa.beschreibung, oa.einheit, oa.anzahl_parameter, oa.su_gewerk, oa.mbs_artikel,
oa.mindermenge_kleiner_als, oa.zulage_mindermengen_artikel_nummer,
ae.p1_einheit AS p1_einheit_description,
ae.p2_einheit AS p2_einheit_description,
ae.p3_einheit AS p3_einheit_description
FROM
objekt_position op
INNER JOIN objekt_artikel oa
ON oa.artikelnummer = op.artikelnummer
AND oa.id_objekt = op.id_objekt
AND oa.id_subunternehmer = op.id_subunternehmer
INNER JOIN artikel_einheit ae
ON ae.bezeichnung = oa.einheit
INNER JOIN subunternehmer s
ON s.subunternehmernummer = op.id_subunternehmer
INNER JOIN subunternehmer_user su
ON su.id_subunternehmer = s.subunternehmernummer
WHERE
su.id_user = 1
AND
op.id_objekt = 1486598
AND
oa.artikelnummer = 172
GROUP BY op.id
ORDER BY
op.position
but it seems that my inner join objekt_artikel are not working. what should i change? because i didnt get a result. But there are results in the table.
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}