Running Inner Join Using Django for two tables

时间:2018-09-18 20:08:13

标签: django oracle

I have two tables QUEUE and MESSAGE. The relationship is One Queue to Many Messages. The Queue table has SEQ field which directly links to Field QUEUE in Message table.

So QUEUE field is a foreign key in QUEUE table, which links to Primary key SEQ field which resides in QUEUE table.

I want to get Messages in a queue with a certain status and have certain age in minutes(calculated from date_create and current time), and the result set must contain a message with the queue name

So views are as follows:

Queue Table Fields: SEQ, NAME

Message Table Fields: MSG_NO, QUEUE, STATUS1, DATE_CREATED

I need two select Messages in the Message table for last 12 hours and turn INNER JOIN Queue name to the results: enter image description here

I want to produce similar result using Django ORM, raw query below:

SELECT m.msg_no, q.seq, To_Char(m.date_created,'yyyy/mm/dd hh24:mi:ss'), To_Char(SYSDATE-30/1440,'yyyy/mm/dd hh24:mi:ss'), q.name, Round((round(SYSDATE - m.date_created,5)*1440),1) AS msg_age  
FROM message m
INNER JOIN queue q ON m.queue = q.seq
WHERE m.date_created > To_Date(To_Char(SYSDATE,'yyyymmdd')||'050000', 'yyyymmddhh24miss')
And m.date_created < (SYSDATE - 30/1440)
AND m.status1 = 0 
AND m.direction = 0

0 个答案:

没有答案