Django Raw查询输出与pgAdmin输出不一致-问答-阿里云开发者社区-阿里云

开发者社区> 一码平川MACHEL> 正文

Django Raw查询输出与pgAdmin输出不一致

2019-01-22 17:27:56 3220 1

我正在使用django和postgres 10 db引擎通过表单构建基于Web的搜索工具。当通过django中的原始SQL执行查询时,我在pgAdmin的Ouery工具中运行相同的SQL时只收到3条记录而不是902条。

我已经尝试使用django ORM来构建我的查询但是示例非常简单,我无法成功地将其用于我的需求。
Django Raw SQL语句

ProstateUpper = 'Prostate'
ProstateLower = 'prostate'
GenderMale = 'Male'
GenderAll = 'All'

Master_Query = Eligibilities.objects.raw('''SELECT DISTINCT ON (Eligibilities.nct_id) Eligibilities.id, Studies.brief_title, Eligibilities.nct_id, Conditions.name, Eligibilities.gender, Eligibilities.minimum_age, Eligibilities.maximum_age, Eligibilities.criteria, Interventions.intervention_type, Interventions.name, Facilities.city, Facilities.state, Facilities.country, Brief_Summaries.description, Facility_Contacts.name, Facility_Contacts.email FROM Eligibilities INNER JOIN Studies on Studies.nct_id = Eligibilities.nct_id INNER JOIN Conditions on Conditions.nct_id = Eligibilities.nct_id INNER JOIN Interventions on Interventions.nct_id = Eligibilities.nct_id INNER JOIN Facilities on Facilities.nct_id = Eligibilities.nct_id INNER JOIN Brief_Summaries on Brief_Summaries.nct_id = Eligibilities.nct_id INNER JOIN Facility_Contacts on Facility_Contacts.nct_id = Eligibilities.nct_id WHERE ((Conditions.name LIKE %s OR Conditions.name LIKE %s)) AND (gender LIKE %s OR gender LIKE %s) ''', [ProstateUpper, ProstateLower, GenderMale, GenderAll])[:1000]
pgAdmin SQL语句

SELECT DISTINCT ON (eligibilities.nct_id) eligibilities.id, studies.brief_title, eligibilities.nct_id, conditions.name, eligibilities.gender, eligibilities.minimum_age, eligibilities.maximum_age, eligibilities.criteria, interventions.intervention_type, interventions.name, facilities.city, facilities.state, facilities.country, brief_summaries.description, facility_contacts.name, facility_contacts.email
FROM ctgov.eligibilities
INNER JOIN ctgov.studies on studies.nct_id = eligibilities.nct_id
INNER JOIN ctgov.conditions on conditions.nct_id = eligibilities.nct_id
INNER JOIN ctgov.interventions on interventions.nct_id = eligibilities.nct_id
INNER JOIN ctgov.facilities on facilities.nct_id = eligibilities.nct_id
INNER JOIN ctgov.brief_summaries on brief_summaries.nct_id = eligibilities.nct_id
INNER JOIN ctgov.facility_contacts on facility_contacts.nct_id = eligibilities.nct_id
WHERE ((conditions.name LIKE '%Prostate%' OR conditions.name LIKE '%prostate%')) AND (gender LIKE '%Male%' OR gender LIKE '%All%')
;
我希望输出与记录的数量和内容相同,因为它们查询相同的源。

取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:26:22

    需要更改提供Where子句的变量以反映Conditions.name LIKE '%prostate%'在查询中,因此:

    ProstateUpper = '%Prostate%'
    ProstateLower = '%prostate%'
    GenderMale = '%Male%'
    GenderAll = '%All%'

    0 0
相关问答

17

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 964173浏览量 回答数 17

145

回答

【新手入门】云服务器linux使用手册

fanyue88888 2012-11-26 17:14:18 157052浏览量 回答数 145

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 333814浏览量 回答数 8

38

回答

[@饭娱咖啡][¥20]对于慢sql有没有什么比较实用的诊断和处理方法?

江小白太白 2018-10-30 18:47:38 141638浏览量 回答数 38

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 146755浏览量 回答数 22

42

回答

【精品问答集锦】Python热门问题

小六码奴 2019-05-30 15:27:34 135471浏览量 回答数 42

38

回答

安全组详解,新手必看教程

我的中国 2017-11-30 15:23:46 256842浏览量 回答数 38

249

回答

阿里云LNAMP(Linux + Nginx + Apache + MySQL + PHP)环境一键安装脚本

云代维 2014-02-14 15:26:06 302571浏览量 回答数 249

24

回答

【精品问答】python技术1000问(1)

问问小秘 2019-11-15 13:25:00 471298浏览量 回答数 24

2

回答

区域选择帮助

fanyue88888 2012-12-07 15:54:30 203988浏览量 回答数 2
47
文章
723
问答
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载