首页 \ 问答 \ 查询查询 - MySQL和PHP(Query a Query - MySQL and PHP)

查询查询 - MySQL和PHP(Query a Query - MySQL and PHP)

我最近试图做一个项目*,这让我问这个问题。 虽然从那以后我找到了另一种解决方案,但我仍然很好奇,如果我想象的那样,无论如何都是可能的。

基本上,我想知道是否有任何方式在php中对MySQL查询结果执行MySQL查询。 例如:

$result = mysql_query("SELECT * FROM foo WHERE bar=".$barValue);

然后,能够对$result执行多个查询:

$newResult = mysql_query("SELECT * FROM $result WHERE otherBar=".$barValue);

要么

$otherNewResult = mysql_query("SELECT * FROM $result WHERE otherOtherBar=".$barValue." ORDER BY foobar ASC");

等等等等...

我意识到我可以使用我的新WHERE语句和ORDER BY s附加原始查询,但这会导致我不必要地查询数据库并且它阻止我编写更多面向对象的代码(因为我无法传递结果是查询,而是必须在每个函数中重新查询数据库...)

任何建议,代码,框架或ramblings都赞赏。

*顺便说一句,我的项目不得不为某些年龄组中出生的人查询大型人员数据库,然后查询这些年龄组的不同人口统计数据。

编辑

不,编写自定义函数来查询数据库不值得它给我的面向对象(和可修改性)


I was recently trying to do a project*, which caused me to ask this question. Although since then I've found an alternative solution, I am still curious if what I envisioned doing is, in any way, possible.

Essentially, I am wondering if there is anyway to perform a MySQL query on a MySQL query result in php. For example:

$result = mysql_query("SELECT * FROM foo WHERE bar=".$barValue);

AND THEN, be able to perform multiple queries on $result:

$newResult = mysql_query("SELECT * FROM $result WHERE otherBar=".$barValue);

OR

$otherNewResult = mysql_query("SELECT * FROM $result WHERE otherOtherBar=".$barValue." ORDER BY foobar ASC");

AND so on and so forth...

I realize that I could append the original query with my new WHERE statements and ORDER BYs, but that causes my to query the database unnecessarily and it prevents me from writing more objected oriented code (because I can't pass around a result to be queried, but rather have to requery the database in every function...)

Any advice, pieces of code, frameworks, or ramblings appreciated.

*BTW, my project was having to query a large database of people for people born in certain age groups and then query those age groups for different demographics.

Edit

No, writing a custom function to query the database is not worth the object-orientation (and modifiability) it would give me


原文:https://stackoverflow.com/questions/6977408
更新时间:2022-07-02 22:07

最满意答案

在Rails 5中,默认情况下需要belongs_to http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html ,所以你会得到一个没有它的验证错误。

在此示例中,您尝试在没有User情况下创建Authentication对象,这就是“验证失败:用户必须存在”的原因。

如果你真的想在没有User的情况下创建Authentication对象,这应该有效:

class Authentication < ActiveRecord::Base
  belongs_to :user, optional: true
end

In Rails 5 belongs_to is required by default http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html, so you'll get a validation error without it.

In this example you are trying to create Authentication object without a User, that's why you get "Validation failed: User must exist".

If you really want to create Authentication object without User, this should work:

class Authentication < ActiveRecord::Base
  belongs_to :user, optional: true
end

相关问答

更多
  • 你的模特应该是这样的: class Product < ActiveRecord::Base has_many :orders end class Order < ActiveRecord::Base belongs_to :product belongs_to :user end 现在你可以这样做: Order.joins(:product).all 但是你想要实现什么目标? 这个毫无意义的加入是什么原因? 如果您只想预加载产品,以便对数据库没有任何其他查询,则可以使用includes而 ...
  • 必须在正在评估的记录对象上调用停止值。 速记版本是传递符号:停止,并且这被认为是记录上的方法,将在验证时间被调用。 否则,你可以像这样使用lambda lambda {|r| r.stop } 我倾向于将你的验证写为 validates_datetime :start validates_datetime :stop, :after => :start ps我是插件作者:) The stop value must be called on the record object being evaluat ...
  • 这应该做的工作: def update @business = Business.find(params[:id]) set_associations end private def set_associations industriesParams = params[:business][:industry_ids].reject{ |c| c.empty? } @business.update(:industry_ids => industriesParams) end 但为了 ...
  • 在Rails 5中,默认情况下需要belongs_to http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html ,所以你会得到一个没有它的验证错误。 在此示例中,您尝试在没有User情况下创建Authentication对象,这就是“验证失败:用户必须存在”的原因。 如果你真的想在没有User的情况下创建Authentication对象,这应该有效: class Aut ...
  • 要回答你的确切问题,答案是“有点是,有点不”。 内部设置关联的rails非常复杂。 是的,涉及getter / setter方法,是的,指向涉及表行的......但这并不是Active Record所做的,而且它不仅仅是Active Record所做的。 如果你真的想知道Active Record里面的内容:你可以去看看github上的源代码 。 如果您有更具体的问题...我建议您更新您的问题,要求:) To answer your exact question, the answer is "kinda ...
  • 瑞恩有一个关于这个很好的railscast 。 Belongs_to不是您添加到迁移中的东西,而是将其添加到模型中。 在迁移中,您必须添加外键列。 例如,如果您有一个属于用户的发布模型,您可以在迁移过程中将user_id列添加到发布主动记录中。 然后你添加 belongs_to :user 在后期模型中分开。 然后,rails将在后台发挥它的魔力,为您提供用户模型中的代理集合。 Ryan has a railscast about this that is pretty good. Belongs_to ...
  • 这里是协会: # In User.rb has_many :recipes # In Recipe.rb belongs_to :user has_and_belongs_to_many :ingredients # In Ingredient.rb has_and_belongs_to_many :recipes 现在,假定有一个recipe_id和两个列recipe_id和ingredient_id 。 所以,就你的例子而言,你可以用这种方式找到酵母的所有食谱: @recipes = Ingred ...
  • 您需要条件验证和自定义属性。 请参阅: http : //edgeguides.rubyonrails.org/active_record_validations.html#conditional-validation 。 这样做会从控制器中获取验证代码并返回到它所属的模型中。 基本上类似于此(示例未经测试)应该起作用: validates :frequency, :presence => true, :if => :has_recurrence def has_recurrence=( yesorno= ...
  • 1)如果将变量传递给partial,它将作为本地,而不是实例: <%= render :partial => 'comments/display', locals: {commentable: @article} %> 2)另请注意,从控制器渲染和从视图渲染是不同的事情。 在控制器中,您不必指定:partial=>因为Rails中的控制器应该呈现整个模板,但是如果您在视图文件中,则应该像Rails一样只能为每个请求呈现一个模板 1) If you pass variable to partial, it ...
  • 你在做什么是错的,property_id不会存储在User表中。 你有一对多的关系。 当单个用户有3个属性然后您将3个property_id存储在该单个用户记录中时会发生什么。 不,除了您在代码中正确执行外,Property表将具有user_id,您将通过在控制器中执行此操作来获取特定用户的所有属性。 @user = User.find(:user_id) @properties = @user.properties What you are doing is wrong, the property_id ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)