how can i left join and add where condition its doesn't read the where it brings all the ridedriver object? - doctrine-orm

How can I LEFT and add WHERE condition?
It doesn't read the where it brings all the ridedriver object?
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE p.frequency IS NOT NULL
AND p.nbrplaces > 0
AND b.iduser != 2
OR b.iduser IS NULL

If I understand your question correctly it is probably an issue with your WHERE clause. Try using parantheses with the OR portion of the clause.
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE p.frequency IS NOT NULL
AND p.nbrplaces > 0
AND (b.iduser != 2 OR b.iduser IS NULL)
To answer the question you asked in the comments..
i want to select all the driverrides except the one that the user already booked in
Sounds like you just do not want the other conditions you have in your WHERE clause.
SELECT p
FROM RideDriverEmployeeBundle:Ridedriver p
LEFT JOIN Chaya3niUserBundle:Bookings b WITH b.idridedriver = p.id
WHERE b.iduser IS NULL
By doing a LEFT JOIN and then only taking records that did not exist in the joined table with WHERE .. IS NULL, this is doing a LEFT OUTER JOIN.
In your case Ridedriver is Table A and Bookings is Table B, so this will return records in Ridedriver that do not join to anything in Bookings.

Related

Update columns based on condition PL/SQL setting them as NULL

I wanted to update columns based on condition: There is this table with columns A, B, C, D, E.
These columns with values are to be updated with null. I do not want to touch the columns without any values.
...
UPDATE
Table
SET
A = CASE WHEN A!=NULL THEN A=NULL
B= CASE WHEN B!=NULL THEN B=NULL
C= CASE WHEN C!=NULL THEN C='U'
D= CASE WHEN D!=NULL THEN D=NULL ELSE END,
WHERE
where condition;
...
PS:: The column values are not same.
Your query was almost close.Below one should work,
update test_Data
set A = CASE WHEN A is not NULL THEN NULL END,
B = CASE WHEN B is not NULL THEN NULL END ,
C = CASE WHEN C is not NULL THEN NULL END,
D = CASE WHEN D is not NULL THEN NULL END,
E = CASE WHEN E = 'abc' THEN 'QWE' ELSE E END
where (A is not NULL or B is not NULL or C is not NULL or D is not null or E is not null)
UPDATE:
If any of the column has NOT NULL Constraint, then column has to be altered to take NULL.
alter table test_Data modify E null;
for demo with example refer DB fiddle link - https://dbfiddle.uk/?rdbms=oracle_18&fiddle=7f8bfc2f5edda71a9eb5af5ccb2065de

Cursor declaration in Amazon Redshift

My aim is to parse the first table check if the ID exists in the second table, then do some updates.
The first table runs in loop and parses each row in the second table to update or give the output.
Here the code that I used:
DECLARE
CURSOR c1 FOR (SELECT pnr,agrnumber,pnrcreatedate FROM test2_view_table);
r1 c1%ROWTYPE;
CURSOR c2 FOR (SELECT pnr,ano,pcdt FROM sdh_ticket_test2_update);
r2 c2%ROWTYPE;
BEGIN
FOR r1 IN c1
LOOP
FOR r2 IN c2
LOOP
IF (r1.pnr = r2.pnr and r2.pnrcreatedate is null and
r1.agrnumber=r2.ano)
THEN
c++
else if (r1.pnr = r2.pnr and r2.agrnumber is null and
r1.pcdt=r2.pnrcreatedate)
THEN
a++ -- continue to the next iteration of the outer loop
-- as we have a match
GOTO continue;
END IF;
END LOOP;
-- we can only reach that point if there was no match
DBMS_OUTPUT.PUT_LINE(TO_CHAR(c));
<<continue>>
NULL;
END LOOP;
END;
For the declaration of cursor in redshift as above. its throwing me the following error.
[Amazon](500310) Invalid operation: syntax error at or near "c1"
Please let me know if there is any solution.
This kind of row-by-row processing is not suitable for any modern database. Just define the logic in a SQL query (or queries) and let the DB figure out how to do the actual work.
In this case you can do the entire comparison in one step. You could actually rewrite this as a single UPDATE.
BEGIN
UPDATE sdh_ticket_test2_update
SET pnrcreatedate = c1.pcdt
FROM test2_view_table c1
LEFT JOIN sdh_ticket_test2_update c2
ON c1.pnr = c2.pnr
AND c1.agrnumber = c2.ano
WHERE c2.pnrcreatedate IS NULL
;
UPDATE sdh_ticket_test2_update
SET ano = c1.agrnumber
FROM test2_view_table c1
LEFT JOIN sdh_ticket_test2_update c2
ON c1.pnr = c2.pnr
AND c1.pcdt = c2.pnrcreatedate
WHERE c2.agrnumber IS NULL
;
COMMIT

libreoffice base left join deletes row

I have the following query in Base:
SELECT field_name1, field_name2, .,.,.
FROM table_a
INNER JOIN table_b
ON table_a.field_name1 = table_b.field_name1
INNER JOIN table_c
ON table_a.field_name2 = table_c.field_name2
INNER JOIN table_d
ON table_d.field_name1 = table_b.field_name2
LEFT JOIN table_e
ON table_e.field_name1 = table_c.field_name2 AND
table_e.field_name2 = table_b.field_name1
When I delete a row from table_a the Base parser also deletes the row from table_e. Adding or modifying a row is ok it is only a problem when it comes to deleting a row. Here is the actual code:
FROM "Futures_Orders"
INNER JOIN "Contract_Details"
ON "Contract_Details"."Symbol" = "Futures_Orders"."Symbol"
INNER JOIN "Broker"
ON "Broker"."Broker" = "Futures_Orders"."Broker"
INNER JOIN "FX_Rates"
ON "FX_Rates"."Code" = "Contract_Details"."Currency"
LEFT JOIN "Broker_Commissions"
ON "Broker_Commissions"."Broker" = "Broker"."Broker" AND
"Broker_Commissions"."Symbol" = "Contract_Details"."Symbol"
In addition to the row from futures_orders a row from Broker_Commissions is deleted.

SELECT Statement within IF statement

I would like to get a different result to my select statement when a parameter is 0, 1 or 2. I am not very skilled in PLSQL so I am not sure if my code would give the expected result. If i run this code i get a "SQL statement ignored" on line 3.
BEGIN
IF (:PARTYPE = 1) THEN
SELECT * FROM x
WHERE to_date(date) >= (Select to_date(sysdate)from DNV.dual)
ELSE
SELECT * FROM x
WHERE to_date(date) <= (Select to_date(sysdate)from DNV.dual)
END IF;
END;
This is just a example of my SELECT statement. Later this statement will become longer and more complex but I think this shows which results I am trying to get.
Below is a copy of my entire code but because I am not allowed to show this it has become very unreadable:
BEGIN
IF (:PARTYPE = 1) THEN
Select table1.Column1
, table1.Column2
, table1.Column3
, table1.Column4
, table1.Column5
, table1.Column6
, table1.Column7
, table1.Column8
, table1.Column9
, table1.Column10
, table1.Column11
, table1.Column12
, (Select table2.ColumnX From x2 table2 Where somthing) as "something" From x1 table1
WHERE to_date(date) >= (Select to_date(sysdate)from DNV.dual)
Order by columnX
ELSE
Select table1.Column1
, table1.Column2
, table1.Column3
, table1.Column4
, table1.Column5
, table1.Column6
, table1.Column7
, table1.Column8
, table1.Column9
, table1.Column10
, table1.Column11
, table1.Column12
, (Select table2.ColumnX From x2 table2 Where somthing) as "something" From x1 table1
WHERE to_date(date) <= (Select to_date(sysdate)from DNV.dual)
Order by columnX
END IF;
END;
I have created some new code with which i am trying to learn how a case statement works. This might help me with the code above. Unfortunately this code also doesn't work but I think it explanes my situation better. In this excample i use a separate table with data i made up. In some cases user2 is null but user1 is always filled. I want to get all items where user2 equals the parameter but if user2 is null and user1 does equal the paramter i still need that item to apear.
Select t1.user1,
t1.user2
From table t1
Where (Case
When t1.user2 IS NULL Then t1.user1 in (:PARUSER)
ELSE t1.user2 in (:PARUSER)
End Case)
Since the relational operator of the where clause depends on the partype, you cannot do the traditional CASE statement charm here. I'll have to resort with this one:
SELECT * FROM x
WHERE (to_date(date) >= (Select to_date(sysdate)from DNV.dual) AND :PARTYPE = 1)
OR (to_date(date) <= (Select to_date(sysdate)from DNV.dual) AND :PARTYPE != 1)

Stop fetching an association

I perform a simple query like this to fetch an association with episodes:
$query = $this->getEntityManager()
->createQuery('
SELECT p,e
FROM AcmeDemoBundle:Place p
LEFT JOIN p.episodes e
WHERE p.id = :id'
)
->setParameter('id',$id);
This is a simple asso:
/**
* #ORM\OneToMany(targetEntity="Episode", mappedBy="place")
*/
protected $episodes;
This works well. Now, I don't want to fetch episodes, but simply the place object (and nothing else):
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM AcmeDemoBundle:Place p
LEFT JOIN p.episodes e
WHERE p.id = :id'
)
->setParameter('id',$id);
This is still loading episodes by lazy-loading. Is there a way to avoid lazy-loading in that case?
Many thanks.
Like this:
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM AcmeDemoBundle:Place p
WHERE p.id = :id'
)
->setParameter('id',$id);
$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);