Left join is not working on codeigniter - mysql

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));

Related

SSRS: Parameter not filtering MySQL query

I have a Date/Time parameter to my report:
But when I run my query, I get no results:
SELECT HD_QUEUE.NAME as qname, HD_TICKET.ID, HD_TICKET.CREATED, HD_TICKET.TIME_CLOSED, CUSTOMER.FULL_NAME as custfullname,
HD_STATUS.NAME as statname, HD_TICKET.TITLE, left(ASSIGNEE.FULL_NAME, 40) as assignee,
HD_PRIORITY.NAME as pname, HD_CATEGORY.NAME as catname
FROM HD_TICKET
INNER JOIN HD_QUEUE
ON HD_TICKET.HD_QUEUE_ID = HD_QUEUE.ID
INNER JOIN USER CUSTOMER
ON HD_TICKET.SUBMITTER_ID=CUSTOMER.ID
INNER JOIN USER ASSIGNEE
ON HD_TICKET.OWNER_ID=ASSIGNEE.ID
INNER JOIN HD_STATUS
ON (HD_TICKET.HD_STATUS_ID=HD_STATUS.ID)
AND (HD_TICKET.HD_QUEUE_ID=HD_STATUS.HD_QUEUE_ID)
INNER JOIN HD_PRIORITY
ON HD_TICKET.HD_PRIORITY_ID = HD_PRIORITY.ID
and HD_TICKET.HD_QUEUE_ID = HD_PRIORITY.HD_QUEUE_ID
INNER JOIN HD_CATEGORY
ON HD_TICKET.HD_CATEGORY_ID = HD_CATEGORY.ID
and HD_TICKET.HD_QUEUE_ID = HD_CATEGORY.HD_QUEUE_ID
left join ASSET on ASSET.ID = HD_TICKET.ASSET_ID
left join ASSET_DATA_6 on ASSET.ASSET_DATA_ID = ASSET_DATA_6.ID
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < #date_param);
What am I doing wrong?
MySQL does not allow named parameters. Use '?' instead of '#date_param' in the query.
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < ?;
Then check the Dataset Properties and make sure the '?' is associated with the value of your parameter:

Mysql Update select statement

I am trying to run an update on the following select statement but i dont think mysql is very fond of my syntax, Please advise?
UPDATE `invoice_lines`
SET
`invoice_lines`.`cost_price` = Costnew,
`invoice_lines`.`list_price` = Listnew,
`invoice_lines`.`unit_price` = Unitnew
SELECT (quote_lines.`list_price` * products.commision_pers/100) AS Listnew,
(quote_lines.`cost_price` * products.commision_pers/100) AS Costnew,
(quote_lines.`unit_price` * products.commision_pers/100) AS Unitnew
FROM `quote_lines`
INNER JOIN quotes
ON quotes.id = quote_lines.`quote_id`
INNER JOIN QuotePers
ON quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.`from_quote_id` = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.`invoice_id` = invoice.id
WHERE products.id = invoice_lines.`related_id`
AND prodid = invoice_lines.related_id
AND invoice_id = invoice.id
GROUP BY quotes.id,products.`id`
Buddy, Try to refer the table names from which those columns come from in where condition and next time you need to make use of table_name aliases while using such big queries :)
UPDATE invoice_lines
SET
invoice_lines.cost_price = Costnew,
invoice_lines.list_price = Listnew,
invoice_lines.unit_price = Unitnew
SELECT (quote_lines.list_price * products.commision_pers/100) AS Listnew,
(quote_lines.cost_price * products.commision_pers/100) AS Costnew,
(quote_lines.unit_price * products.commision_pers/100) AS Unitnew
FROM quote_lines
INNER JOIN quotes
ON quotes.id = quote_lines.quote_id
INNER JOIN QuotePers
ON quote_lines.quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.from_quote_id = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.invoice_id = invoice.id
WHERE products.id = invoice_lines.related_id
AND quote_lines.prodid = invoice_lines.related_id
AND quote_lines.invoice_id = invoice.id
GROUP BY quotes.id,products.id
Try this, I can not test it as I don't have your database schema:
UPDATE `invoice_lines`
INNER JOIN quotes
ON quotes.id = quote_lines.`quote_id`
INNER JOIN QuotePers
ON quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.`from_quote_id` = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.`invoice_id` = invoice.id
WHERE products.id = invoice_lines.`related_id`
AND prodid = invoice_lines.related_id
AND invoice_id = invoice.id
SET
`invoice_lines`.`cost_price` = (quote_lines.`cost_price` * products.commision_pers/100),
`invoice_lines`.`list_price` = (quote_lines.`list_price` * products.commision_pers/100),
`invoice_lines`.`unit_price` = (quote_lines.`unit_price` * products.commision_pers/100)
Hope it gets you on the right track.
Also check this related question

MySQL JOIN Multiple tables not working

I'm not sure why this isn't working. If I join either table separately, it comes back with the appropriate results, but when I try to join them both, I get 0 results. (car_id and boat_id are both primary keys on their tables.)
$query = "SELECT
*
FROM
posted c
JOIN posted_car e on c.car_id = e.car_id
JOIN posted_boat g on c.boat_id = g.boat_id
WHERE
c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));
Might be worth noting that when I do
LEFT JOIN posted_car e on c.car_id = e.car_id
RIGHT JOIN posted_boat g on c.boat_id = g.boat_id
I get results as if I had only joined the posted_boat table. If anyone could point me in the right direction...it would be much appreciated.
you are using JOIN that might be a problem . you should use left outer join to get proper result . check following syntax :
$query = "SELECT *
FROM
posted c
left OUTER JOIN posted_car e on c.car_id = e.car_id
left OUTER JOIN posted_boat g on c.boat_id = g.boat_id
WHERE c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));

IF/ELSE LEFT JOIN

I need to do a LEFT JOIN with IF/ELSE, this is my query:
IF (M.idArtVar=null,
LEFT JOIN ArtMaga G
ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo),
LEFT JOIN ArtMaga G
ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo AND
G.idArtVar = M.idArtVar)
)
But it doesn't work.
I also tried like this:
LEFT JOIN ArtMaga AM
ON IF(M.idArtVar IS NULL,
(AM.idMagazzino = TM.idMagazzino AND AM.idArticolo = A.idArticoli),
(AM.idMagazzino = TM.idMagazzino AND AM.idArtVar = M.idArtVar))
But this query is too slow.
How can I do?
Thanks.
EDIT: This is full query:
SELECT F.Codice AS "CodiceFornitore", F.RagioneSociale AS "RagioneSocialeFornitore", A.ArticoloFornitore, C.Descrizione AS CatDes, S.Descrizione AS Settore, U.Sigla AS Um, U2.Sigla AS Um2, A.Moltiplicatore AS Molt, A.Collo, TM.
dMagazzino, M.idArtVar, AM.Esistenza, AM.Disponibilita, AM.QtaImpegnata, AM.QtaOrdinata, TM.TipoSoggetto, TM.idSoggetto, ST.DataMovimento, MC.Codice, ST.Quantita, ST.Prezzo, ST.Sconti, M.idMagaRigMov
FROM MagaRigMov M
LEFT JOIN Articoli A ON A.idArticoli = M.idArticolo
LEFT JOIN UnMisura U ON U.idUnMisura = A.idUnMisura1
LEFT JOIN UnMisura U2 ON U2.idUnMisura = A.idUnMisura2
LEFT JOIN Iva I ON I.idIva = A.idIva
LEFT JOIN Settori S ON S.idSettori = A.idSettore
LEFT JOIN Fornitori F ON F.idFornitori = A.idFornitore
LEFT JOIN ArtCategorie C ON C.idArtCategorie = A.idArtCategoria
LEFT JOIN MagaTesMov TM ON TM.idMagaTesMov = M.idMagaTesMov
LEFT JOIN STORICO ST ON (ST.idSoggetto = TM.idSoggetto AND ST.TipoSoggetto = TM.TipoSoggetto AND ST.idArticolo = M.idArticolo)
LEFT JOIN MagaCausali MC ON MC.idMagaCausali = ST.idMagaCausale
LEFT JOIN ArtMaga AM ON IF(M.idArtVar IS NULL,(AM.idMagazzino = TM.idMagazzino AND AM.idArticolo = A.idArticoli),
(AM.idMagazzino = TM.idMagazzino AND AM.idArtVar = M.idArtVar))
This query is too slow.. but works..
You can't use an IF to make a conditional join. Because IF is not part of the SELECT syntax and even if it was (like CASE expressions) it wouldn't be allowed to be used like this. You can move the logic to the ON statement though:
LEFT JOIN ArtMaga G
ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo)
AND M.idArtVar IS NULL
OR (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo AND
G.idArtVar = M.idArtVar)
AND M.idArtVar IS NOT NULL
which can be simplified to:
LEFT JOIN ArtMaga G
ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo)
AND (M.idArtVar IS NULL OR G.idArtVar = M.idArtVar)
Also notice that you can't use equality to check if an expression is null.
M.idArtVar = null will never be true because NULL can never be equal to anything (not even to NULL). The way to check if an expression is null is with IS NULL.
Your second query, that words, is using the IF() function of MySQL and seems to be correct (although I see a difference in the code with the first query, the G.idArticolo = M.idArticolo condition has been removed from one part.)
Why a query is slow depends on many factors and using functions on the join conditions can be one of the many. Try the change I suggest above. If it still slow, you'll have to examine the execution plan and the available indexes on the tables.
Just put the condition in the on clause. If is not part of a SQL statement.
SELECT F.Codice AS "CodiceFornitore", F.RagioneSociale AS "RagioneSocialeFornitore",
A.ArticoloFornitore, C.Descrizione AS CatDes, S.Descrizione AS Settore, U.Sigla AS Um, U2.Sigla AS Um2, A.Moltiplicatore AS Molt, A.Collo, TM.
dMagazzino, M.idArtVar, AM.Esistenza, AM.Disponibilita, AM.QtaImpegnata, AM.QtaOrdinata, TM.TipoSoggetto, TM.idSoggetto, ST.DataMovimento, MC.Codice, ST.Quantita, ST.Prezzo, ST.Sconti, M.idMagaRigMov
FROM MagaRigMov M
LEFT JOIN Articoli A ON A.idArticoli = M.idArticolo
LEFT JOIN UnMisura U ON U.idUnMisura = A.idUnMisura1
LEFT JOIN UnMisura U2 ON U2.idUnMisura = A.idUnMisura2
LEFT JOIN Iva I ON I.idIva = A.idIva
LEFT JOIN Settori S ON S.idSettori = A.idSettore
LEFT JOIN Fornitori F ON F.idFornitori = A.idFornitore
LEFT JOIN ArtCategorie C ON C.idArtCategorie = A.idArtCategoria
LEFT JOIN MagaTesMov TM ON TM.idMagaTesMov = M.idMagaTesMov
LEFT JOIN STORICO ST ON (ST.idSoggetto = TM.idSoggetto AND ST.TipoSoggetto = TM.TipoSoggetto AND ST.idArticolo = M.idArticolo)
LEFT JOIN MagaCausali MC ON MC.idMagaCausali = ST.idMagaCausale
LEFT JOIN ArtMaga AM ON (AM.idMagazzino = TM.idMagazzino AND (m.idartVar is NULL and AM.idArtVar = M.idArtVar or AM.idArticolo = A.idArticoli))

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}