Search
 
SCRIPT & CODE EXAMPLE
 

SQL

raw query must include primary key

queryset=country_model.objects.raw('SELECT myapp_country_model.cname,myapp_state_model.sname FROM myapp_country_model INNER JOIN myapp_state_model on myapp_country_model.id=myapp_state_model.cid_id')
Solution
queryset=country_model.objects.raw('SELECT myapp_country_model.id,myapp_country_model.cname,myapp_state_model.sname FROM myapp_country_model INNER JOIN myapp_state_model on myapp_country_model.id=myapp_state_model.cid_id ')
Comment

Raw query must include the primary key

Include 1 as id to your query

Message.objects.raw('''
        SELECT 1 as id , s1.ID, s1.CHARACTER_ID, MAX(s1.MESSAGE) MESSAGE, MAX(s1.c) occurrences
        FROM
           (SELECT ID, CHARACTER_ID, MESSAGE, COUNT(*) c
            FROM tbl_message WHERE ts > DATE_SUB(NOW(), INTERVAL %s DAY) GROUP BY CHARACTER_ID,MESSAGE) s1
        LEFT JOIN
           (SELECT ID, CHARACTER_ID, MESSAGE, COUNT(*) c
            FROM tbl_message WHERE ts > DATE_SUB(NOW(), INTERVAL %s DAY) GROUP BY CHARACTER_ID,MESSAGE) s2
          ON s1.CHARACTER_ID=s2.CHARACTER_ID
         AND s1.c < s2.c
        WHERE s2.c IS NULL
        GROUP BY CHARACTER_ID
        ORDER BY occurrences DESC''', [days, days]) 
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql select greater than yesterday 
Sql :: oracle drop chain step 
Sql :: psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" f 
Sql :: drop db syntax 
Sql :: alter sequence set current value oracle 
Sql :: ORACLE RIGHT TWO DIGITS 
Sql :: delete a view sql 
Sql :: sql server reset auto increment 
Sql :: alert table name mysql 
Sql :: postgresql reset auto_increment index 
Sql :: SQL select past number of days 
Sql :: sql throw error 
Sql :: oracle sql assign unique number to each grouping 
Sql :: left join with only first record 
Sql :: error code 1292 mysql workbench 
Sql :: encoding UTF8 has no equivalent in encoding WIN1252 
Sql :: table information in sql server 
Sql :: oracle get nls settings 
Sql :: Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates. 
Sql :: sql list users and roles 
Sql :: get parameter value in mysql trigger 
Sql :: how to get the ddl for datafile in oracle tablespace 
Sql :: default password of mysql 
Sql :: sql get number of days between two dates 
Sql :: get unix time roblox 
Sql :: sql fill na with 0 
Sql :: python mysql search 
Sql :: mysql sysdate 
Sql :: list foreign data tables postgres psql 
Sql :: script to add datafile to tablespace 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =