我有 3 个表。寻找一种使用三个不同表格查找字段 PRICE 差异的好方法,然后显示前 3 个最大的负差异。我想首先找到最好的 MySQL 查询来使用,然后找到最好的方式在 php 中显示它。
可维护:
COMPANY | MODEL | PRICE
Main Company | ProductA | 100.00
Main Company | ProductB | 50.00
Main Company | ProductC | 25.00
Main Company | ProductD | 300.00
兼容 1:
COMPANY | MODEL | PRICE
Competitor1 | ProductA | 100.00 //0
Competitor1 | ProductB | 55.00 //5
Competitor1 | ProductC | 50.00 //25
Competitor1 | ProductD | 200.00 //-100
兼容 2:
COMPANY | MODEL | PRICE
Competitor2 | ProductA | 99.00 //-1
Competitor2 | ProductB | 44.00 //-6
Competitor2 | ProductC | 20.00 //-5
Competitor2 | ProductD | 100.00 //-200
因此,我希望在我的页面中显示的 PRICE 的最大负差异是:
IDEA: 我不太熟悉它,但我可以在三个表上使用 ..UNION SELECT WHERE MODEL=XXX。我可能会循环遍历每一个收集数据、计算和吐出信息的方法。唯一的问题是,我不知道如何将每个变量存储为每个表的自己的价格。此外,我认为它会显示所有差异,除非有一种方法可以在完成数学运算后存储每个变量,然后显示前 3 个差异。
任何能最好地解决此查询的想法或建议都将不胜感激。 (注意:不,我不能将它们全部放在一张表中=p)
最佳答案
无法在 PHP 方面提供帮助,但此查询应该可以满足您的需求。您必须进行合并才能获得所有合格的结果。这将使所有列可用并预先计算,以便您以任何需要的方式放入简单的网格列表中。由于计算是竞争对手与主要公司的比较,通过自然排序的价格差异将首先具有最大的负值,然后变为正值。因此,LIMIT 命令将在订购后应用,并且只发送回 3 条记录。
select
MT.Model,
MT.Company as MainCompany,
MT.Price as MainPrice,
CT1.Company as Competitor,
CT1.Price as CompPrice,
CT1.Price - MT.Price as PriceDifference
from
MainTable MT
JOIN CompTable1 CT1
on MT.Model = CT1.Model
UNION
select
MT.Model,
MT.Company as MainCompany,
MT.Price as MainPrice,
CT2.Company as Competitor,
CT2.Price as CompPrice,
CT2.Price - MT.Price as PriceDifference
from
MainTable MT
JOIN CompTable2 CT2
on MT.Model = CT2.Model
order by
PriceDifference
limit 3
建议...从长远来看,您构建表格的方式确实很糟糕。您应该尝试规范化数据以获得更佳性能。如果您有 100 个竞争对手,会发生什么情况。你到处都有重复。也更改模型名称。以下是我将如何重组表格……不是显式数据类型,而是概念上的
COMPANY
CompanyID auto-increment
CompanyName character
PRODUCT
ProductID auto-increment
ProductModel character
VendorPricing
VPriceID auto-increment
CompanyID (ID pointing to company table -- to get name when needed)
ProductID (ID pointing to product table -- to get model name too)
Price actual price for this particular company and product
然后,有了适当的索引,如果您想获得从一个供应商到另一个供应商的定价,无论是什么型号,您的查询将来都可以更容易扩展......就像
select
VP1.CompanyID,
C1.CompanyName as MainCompany,
C2.CompanyName as Competitor,
P1.ProductModel,
VP1.Price as MainPrice,
VP2.Price as CompetitorPrice,
VP2.Price - VP1.Price as PriceDifference
from
VendorPricing VP1
JOIN Company C1
on VP1.CompanyID = C1.CompanyID
JOIN Product P1
on VP1.ProductID = P1.ProductID
JOIN VendorPricing VP2
on VP1.ProductID = VP2.ProductID
AND NOT VP1.CompanyID = VP2.CompanyID
JOIN Company C2
on VP2.CompanyID = C2.CompanyID
where
VP1.CompanyID = TheOneCompanyYouAreInterestedIn
order by
PriceDifference
limit 3
现在,如果您有 2、5、10 或 100 个竞争对手,则查询完全相同。
关于php - UNION SELECT MySQL 查询,用 PHP 做数学运算和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10320247/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]