I'm trying to use SQL query where the value of one query is used in another query. Here is my SQL query:
Select *
from ( select detection_class, detection_class_id, matched_alert_id, stream_id, track_id, detection_time, frame_id
from matched_alert
where stream_id = %s
group by track_id )
where (SELECT ',' || detection_class || ',' FROM alerts WHERE alert_id = %s) LIKE '%,' || detection_class || ',%'
this query is stored in query
variable.
then execute as below:
place_holders = [stream_id, alert_id]
try:
with connection.cursor() as cursor:
cursor.execute(query, place_holders)
rows = cursor.fetchall()
shows error:
return sql % params
TypeError: not enough arguments for format string
the result of select detection_class from alerts where alert_id = %s
is like 'car,bus,bike'
but I need result like 'bus', 'car', 'bike'
to give input of where detection_class IN
like where detection_class IN ('car', 'bus')
.
alerts table
matched_alert table
so, how can I split this result by a comma and make a separate string?