这是我正在尝试执行的代码
join = "\n LEFT OUTER JOIN party_identifier on party.id = party_identifier.party_id\n LEFT OUTER JOIN blood_type on blood_type.id = party.blood_type_id\n LEFT OUTER JOIN education_level on education_level.id = party.education_level_id\n LEFT OUTER JOIN religion on religion.id = party.religion_id\n LEFT OUTER JOIN living_arrangement on living_arrangement.id = party.living_arrangement_id\n\t LEFT OUTER JOIN patient_medical_procedure procedures1 on procedures1.patient_id = party.id and procedures1.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem1 on patient_problem1.patient_id = party.id and patient_problem1.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures2 on procedures2.patient_id = party.id and procedures2.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem2 on patient_problem2.patient_id = party.id and patient_problem2.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures3 on procedures3.patient_id = party.id and procedures3.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem3 on patient_problem3.patient_id = party.id and patient_problem3.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures4 on procedures4.patient_id = party.id and procedures4.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem4 on patient_problem4.patient_id = party.id and patient_problem4.record_status_id = 1 \n \n \n WHERE party.type = \"Patient\" AND party.record_status_id = 1 AND party_identifier.source_note = 'simplifyMD Patient ID'\n "
c = " party.birth_date BETWEEN '1948-09-18' AND '1995-09-17' and procedures4.occurrence_date > '2011-01-01 00:00:00' and procedures4.occurrence_date < '2012-12-31 23:59:59' and procedures4.code is not null and procedures4.code in ('99201') and not ( patient_problem4.patient_problem_status_id in (5) and patient_problem4.code is not null and patient_problem4.code in ('062.3') ) and party.id not in ( select patient_problem.patient_id from patient_problem where ( patient_problem.code in ('062.3') ) and ( patient_problem4.patient_problem_status_id in (5) ) ) "
Party.find(:all,:joins=>join,:conditions=>c)
获得以下错误
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ( party.birth_date BETWEEN '1948-09-18' AND '1995-09-17' and procedures' at line 11: SELECT `party`.* FROM `party`
LEFT OUTER JOIN party_identifier on party.id = party_identifier.party_id
LEFT OUTER JOIN blood_type on blood_type.id = party.blood_type_id
LEFT OUTER JOIN education_level on education_level.id = party.education_level_id
LEFT OUTER JOIN religion on religion.id = party.religion_id
LEFT OUTER JOIN living_arrangement on living_arrangement.id = party.living_arrangement_id
LEFT OUTER JOIN patient_medical_procedure procedures1 on procedures1.patient_id = party.id and procedures1.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem1 on patient_problem1.patient_id = party.id and patient_problem1.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures2 on procedures2.patient_id = party.id and procedures2.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem2 on patient_problem2.patient_id = party.id and patient_problem2.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures3 on procedures3.patient_id = party.id and procedures3.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem3 on patient_problem3.patient_id = party.id and patient_problem3.record_status_id = 1 LEFT OUTER JOIN patient_medical_procedure procedures4 on procedures4.patient_id = party.id and procedures4.record_status_id = 1 LEFT OUTER JOIN patient_problem patient_problem4 on patient_problem4.patient_id = party.id and patient_problem4.record_status_id = 1
WHERE party.type = "Patient" AND party.record_status_id = 1 AND party_identifier.source_note = 'simplifyMD Patient ID'
WHERE ( party.birth_date BETWEEN '1948-09-18' AND '1995-09-17' and procedures4.occurrence_date > '2011-01-01 00:00:00' and procedures4.occurrence_date < '2012-12-31 23:59:59' and procedures4.code is not null and procedures4.code in ('99201') and not ( patient_problem4.patient_problem_status_id in (5) and patient_problem4.code is not null and patient_problem4.code in ('062.3') ) and party.id not in ( select patient_problem.patient_id from patient_problem where ( patient_problem.code in ('062.3') ) and ( patient_problem4.patient_problem_status_id in (5) ) ) ) AND (party.record_status_id = 1)
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `log'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/mysql_adapter.rb:324:in `execute'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/mysql_adapter.rb:639:in `select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/base.rb:665:in `find_by_sql'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/base.rb:1582:in `find_any_status_every'
from /opt/simplifyMD/vendor/plugins/acts_as_statused/lib/acts_as_statused.rb:127:in `find_every'
from /opt/simplifyMD/vendor/plugins/acts_as_statused/lib/acts_as_statused.rb:128:in `call'
from /opt/simplifyMD/vendor/plugins/acts_as_statused/lib/acts_as_statused.rb:128:in `find_every'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/base.rb:2182:in `with_scope'
from /opt/simplifyMD/vendor/plugins/acts_as_statused/lib/acts_as_statused.rb:121:in `with_disabled_scope'
from /opt/simplifyMD/vendor/plugins/acts_as_statused/lib/acts_as_statused.rb:128:in `find_every'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/base.rb:619:in `find'
from (irb):70
没有错误代码,查询似乎对我而言,但这里有什么问题?
答案 0 :(得分:1)
您的查询中有两个where子句。一个是连接,另一个是Rails,当你这样做时:conditions =&gt; C。只需修好它们