I want to perform a WHERE - IN query/operation but normal where gives error.
I want this
select * from `calendar_event_rsvp` where `event_id` in ('1', '2', '3')
But below code leads to
select * from `calendar_event_rsvp` where `event_id in` = '1', '2', '3'
Code
CalendarEventRSVP.forge()
.where({
"event_id": event_ids
})
How do i do this in bookshelf.js
Try to add the operator:
CalendarEventRSVP.forge()
.where('event_id', 'in', event_ids)
Or use knex's whereIn:
CalendarEventRSVP.forge()
.query({whereIn: {event_id: event_ids}})
try query() function on your model.
CalendarEventRSVP.query(function(qb){
qb.where('event_id' , 'in' , [1,2,3,4]) ;
})
.fetchAll()
.then();
Related
I am working on the 8 Week SQL Challenge with the following data. I am using Python and SQL Lite.
c.execute('DROP TABLE IF EXISTS runner_orders')
c.execute('CREATE TABLE runner_orders (order_id INTEGER, runner_id INTEGER, pickup_time VARCHAR(19), distance VARCHAR(7), duration VARCHAR(10), cancellation VARCHAR(23))')
more_order2 = [ ('1', '1', '2020-01-01 18:15:34', '20km', '32 minutes', ''),
('2', '1', '2020-01-01 19:10:54', '20km', '27 minutes', ''),
('3', '1', '2020-01-03 00:12:37', '13.4km', '20 mins', 'NULL'),
('4', '2', '2020-01-04 13:53:03', '23.4', '40', 'NULL'),
('5', '3', '2020-01-08 21:10:57', '10', '15', 'NULL'),
('6', '3', 'null', 'null', 'null', 'Restaurant Cancellation'),
('7', '2', '2020-01-08 21:30:45', '25km', '25mins', 'null'),
('8', '2', '2020-01-10 00:15:02', '23.4 km', '15 minute', 'null'),
('9', '2', 'null', 'null', 'null', 'Customer Cancellation'),
('10', '1', '2020-01-11 18:50:20', '10km', '10minutes', 'null')]
c.executemany('INSERT INTO runner_orders VALUES (?,?,?,?,?,?)', more_order2)
c.execute(query)
conn.commit()
I am trying to replace the '' values with null with this code:
query = '''
UPDATE runner_orders
SET cancellation = null
WHERE cancellation IN ('NULL', '')
'''
c.execute(query)
query2 = '''
SELECT * FROM runner_orders
'''
for match in c.execute(query2):
print(match)
For some reason, when I output the values, I got None in all the NULL spaces and it does not replace them with null. How do I fix this?
null is not a string. SQL uses trinary logic: true, false, and null. SQL null roughly means "unknown" or "no value".
If you look at your table in the SQLite client you should see the value is null. Python is translating SQL null into the Python equivalent of None.
I have a complex query which group bys multiple columns, this is an example
$queryBuilder
->join('e.something', 's')
->where('...')
->addGroupBy('e.id s.someField');
Using ->getQuery()->getResults() returns me an array of entities like I'd expect.
[
0 => App\Entity\ExampleEntity,
1 => App\Entity\ExampleEntity
]
If I try to count the results returned using a select like so:
$queryBuilder
->select('COUNT(e.id)')
->join('e.something', 's')
->where('...')
->addGroupBy('e.id s.someField');
I am returned an array of arrays, inside each array is the count. This isn't what I want. Removing the group by I'm given the correct result however the group by is required.
[
[
1 => '11',
],
[
1 => '4',
]
]
I'm stuck on how I can count the results of the group by. I have tried using distinct and I also do not want to count in PHP.
How i can transform 'a,b,c' like 'a','b','c' with plsql, because in my procedure i need make a "where" using "in" but i can't pass 'a','b','c' when I call my procedure
My procedure is below
CREATE OR REPLACE PACKAGE BODY G_JSON_RAW AS
FUNCTION GET_IMS_DATA(P_FECHA_DESDE IN VARCHAR2,
P_FECHA_HASTA IN VARCHAR2,
P_CLASE IN VARCHAR2,
P_NOMBRE_PROYECTO IN VARCHAR2)
RETURN NOKIA_JSON_RAW_TAB PIPELINED IS
BEGIN
FOR DATO IN (
SELECT /*+ MATERIALIZE PARALLEL(NOKIA_IMS_JSON_RAW,2) */
RT.NOMBRE_PROYECTO,JT.TIPO_JS,JT.FECHA_JS,JT.REGIONAL,JT.INTERVALO,JT.ELEMENTO_1,
JT.ELEMENTO_2,JT.CLAVE,JT.VALOR,JT.REFERENTE
FROM
NOKIA_IMS_JSON_RAW RT,
JSON_TABLE(DOCUMENTO FORMAT JSON,'$'
COLUMNS
TIPO_JS VARCHAR2(19 CHAR) PATH '$.tipo',
FECHA_JS VARCHAR2(100 CHAR) PATH '$.fecha',
REGIONAL VARCHAR2(5 CHAR) PATH '$.regional',
INTERVALO NUMBER PATH '$.intervalo',
ELEMENTO_1 VARCHAR2(500 CHAR)PATH '$.elemento_1',
ELEMENTO_2 VARCHAR2(500 CHAR)PATH '$.elemento_2',
NESTED PATH '$.referencias[*]'
COLUMNS(
NESTED PATH '$.datos[*]'
COLUMNS(
CLAVE VARCHAR2(32)PATH '$.clave',
VALOR VARCHAR2(32)PATH '$.valor'
),
REFERENTE VARCHAR2(100)PATH '$.referente'
)
)
JT
WHERE CLASE IN (SELECT * FROM sys.dbms_debug_vc2coll(P_CLASE))
AND NOMBRE_PROYECTO = P_NOMBRE_PROYECTO
AND FECHA BETWEEN TO_DATE(P_FECHA_DESDE,'DD.MM.YYYY HH24')
AND TO_DATE(P_FECHA_HASTA,'DD.MM.YYYY HH24') + 3599/86400) LOOP
PIPE ROW(NOKIA_JSON_RAW_REC(NOMBRE_PROYECTO => DATO.NOMBRE_PROYECTO,
TIPO => DATO.TIPO_JS,
FECHA => DATO.FECHA_JS,
REGIONAL => DATO.REGIONAL,
INTERVALO => DATO.INTERVALO,
ELEMENTO_1 => DATO.ELEMENTO_1,
ELEMENTO_2 => DATO.ELEMENTO_2,
CLAVE => DATO.CLAVE,
VALOR => DATO.VALOR,
REFERENTE => DATO.REFERENTE));
END LOOP;
RETURN;
END GET_IMS_DATA;
--**--**--**--**--
FUNCTION GET_IMS_DATA_RAW(P_FECHA_DESDE IN VARCHAR2,
P_FECHA_HASTA IN VARCHAR2,
P_CLASE IN VARCHAR2,
P_NOMBRE_PROYECTO IN VARCHAR2,
P_INTERVALO IN NUMBER,
P_MEDICION IN VARCHAR2)
RETURN NOKIA_JSON_RAW_TAB PIPELINED IS
--
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD.MM.YYYY HH24:MI:SS''';
--
FOR DATO IN (SELECT /*+ MATERIALIZE PARALLEL(2) */
NOMBRE_PROYECTO,TIPO,FECHA,REGIONAL,INTERVALO,ELEMENTO_1,ELEMENTO_2,CLAVE,VALOR,REFERENTE
FROM GET_IMS_DATA(P_FECHA_DESDE,P_FECHA_HASTA,P_CLASE,P_NOMBRE_PROYECTO)
WHERE REFERENTE IN(SELECT * FROM sys.dbms_debug_vc2coll(P_MEDICION))
AND INTERVALO = P_INTERVALO
) LOOP
PIPE ROW(NOKIA_JSON_RAW_REC(NOMBRE_PROYECTO => DATO.NOMBRE_PROYECTO,
TIPO => DATO.TIPO,
FECHA => DATO.FECHA,
REGIONAL => DATO.REGIONAL,
INTERVALO => DATO.INTERVALO,
ELEMENTO_1 => DATO.ELEMENTO_1,
ELEMENTO_2 => DATO.ELEMENTO_2,
CLAVE => DATO.CLAVE,
VALOR => DATO.VALOR,
REFERENTE => DATO.REFERENTE));
END LOOP;
RETURN;
EXCEPTION
WHEN NO_DATA_NEEDED THEN NULL;
WHEN OTHERS THEN
G_ERROR_LOG_NEW.P_LOG_ERROR('G_JSON_RAW.GET_IMS_DATA_RAW',
SQLCODE,
SQLERRM,
'P_FECHA_DESDE => ' ||P_FECHA_DESDE||
' P_FECHA_HASTA => ' ||P_FECHA_HASTA||
' P_CLASE => ' ||P_CLASE||
' P_NOMBRE_PROYECTO => '||P_NOMBRE_PROYECTO);
END GET_IMS_DATA_RAW;
END G_JSON_RAW;
Probably you can use something like this
with data as
(select 'a,b,c' col
from dual)
select regexp_substr(col, '[^,]+', 1, level) result
from data
connect by level <= length(regexp_replace(col, '[^,]+')) + 1;
The above will produce an output -
RESULT
a
b
c
this is what you can use in your IN query
If I understand thiis case right, you want to build a function, to use in an SQL statements WHERE clause. One way of doing this is to use a pipelined table function, looking at your code, I think you know, what that is.
So you'll need to convert 'a,b,c' into an array, holding the values 'a', 'b' and 'c'. This can be accomplished with a WHILE loop, SUBSTR and INSTR.
CREATE FUNCTION convert_for_in(p_string IN VARCHAR2)
RETURN <TableType of VARCHAR(1)> PIPELINED IS
BEGIN
FOR no_chr IN 1 .. (LENGTH (p_string) + 1) / 2 LOOP
PIPE_ROW (SUBSTR(p_string, 2 * no_chr - 1, 1);
END LOOP;
END;
and add to the WHERE Clause
AND ... IN (SELECT * from table (convert_for_in('a,b,c')))
Does this help?
THANKS , i can resolve my problem
this stayed :
select regexp_substr(P_CLASE, '[^,]+', 1, level) result
from DUAL
connect by level <= length(regexp_replace(P_CLASE, '[^,]+')) + 1
I'm trying to execute an inner query and set parameters without success, i always get:
Invalid parameter number: number of bound variables does not match number of tokens
This is my query :
SELECT i2
FROM MyBundle:Identity i2
INNER JOIN MyBundle:Property\\Mapped\\Email propertyEmail WITH propertyEmail.identity = i2
INNER JOIN MyBundle:Channel channel WITH propertyEmail.channel = channel
WHERE (
SELECT COUNT(mappedEmail)
FROM MyBundle:Property\\Mapped\\Email mappedEmail
WHERE mappedEmail.identity = i2
AND mappedEmail.channel = :channel
AND mappedEmail.createdAt <= :dateFrom
) > 0
AND IDENTITY(propertyEmail.channel) <> :channel
AND propertyEmail.createdAt <= :dateFrom
GROUP BY i2
And this is how i put as subquery:
$innerQuery = $queryBuilder->getEntityManager()->createQuery($query)
->setParameters([
'channel' => $channelId,
'dateFrom' => $dateFrom
])
;
$queryBuilder->andWhere($queryBuilder->expr()->notIn($alias, $innerQuery->getDQL()));
That return the error :
Invalid parameter number: number of bound variables does not match number of tokens
If I execute the query as single query all works right.
Thanks for suggestion.
Try to apply the parameters to the main query as follow:
$innerQuery = $queryBuilder->getEntityManager()->createQuery($query);
$queryBuilder
->andWhere($queryBuilder->expr()->notIn($alias, $innerQuery->getDQL()))
->setParameters([
'channel' => $channelId,
'dateFrom' => $dateFrom
]);
Table1(identity1, a1,b1)
Table2(identity, foreign(identity1),foreign(identity1))
Now when I use this query
$query = $qb->select('a1', ' b1')
->from('table2', 'm1')
->join('ApiMapBundle:tabl1, 'u1', 'WITH', $qb->expr()->orX('m1. foreign(a1) =u1.identity', 'm1. foreign(b1) = u1.identity '))
->andWhere('m1.identity=:tt')
->setParameter('tt', $cn)
->getQuery()
->getResult();
Now the problem with this query is that sometimes it gives id let's tt:5 so it gives me a value like this
Array(
0 => Array(id => 1, b1=> 8000225),
1 => Array(id => 9, b1 => 8000234)) given).
Basically in table the values structure is like this
Table2(Identity=5,foreign1=9,foreign2=1)
Any idea that how can I exactly get the given structure? Because in some cases it is fine they give me proper foreign 1 and foreign 2 but in other cases it make it alternative. Any idea?