查询查询 - 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 andORDER BY
s, 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
最满意答案
在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 aUser
, 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关联(Understanding Rails associations)[2023-05-30]
要回答你的确切问题,答案是“有点是,有点不”。 内部设置关联的rails非常复杂。 是的,涉及getter / setter方法,是的,指向涉及表行的......但这并不是Active Record所做的,而且它不仅仅是Active Record所做的。 如果你真的想知道Active Record里面的内容:你可以去看看github上的源代码 。 如果您有更具体的问题...我建议您更新您的问题,要求:) To answer your exact question, the answer is "kinda ... -
Rails中的多态关联(Polymorphic Associations in Rails)[2022-04-30]
瑞恩有一个关于这个很好的railscast 。 Belongs_to不是您添加到迁移中的东西,而是将其添加到模型中。 在迁移中,您必须添加外键列。 例如,如果您有一个属于用户的发布模型,您可以在迁移过程中将user_id列添加到发布主动记录中。 然后你添加 belongs_to :user 在后期模型中分开。 然后,rails将在后台发挥它的魔力,为您提供用户模型中的代理集合。 Ryan has a railscast about this that is pretty good. Belongs_to ... -
Rails协会(Rails Associations)[2022-05-17]
这里是协会: # 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 ... -
Rails中的模型4在不包含它们时验证可选关联(Model in Rails 4 validating optional associations when they are not included)[2022-02-05]
您需要条件验证和自定义属性。 请参阅: 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 ...