【问题标题】:Coldfusion Merge Results from database with store info and store hours, I know I'm missing itColdfusion 将数据库中的结果与商店信息和商店营业时间合并,我知道我错过了
【发布时间】:2021-10-03 22:18:35
【问题描述】:

至少可以说,我对 QofQ 的体验是有限的。我真的可以使用一些帮助,而不是给任何借口。先感谢您。为了让事情更清楚,我摆脱了许多简单的搜索要求,但不知不觉 20 并不是选择企业的唯一标准。

我有两个数据库,一个用于存储商店及其信息,另一个用于存储营业时间。理想情况下,我想将商店的结果与商店营业时间合并。注意:并非所有商店都有营业时间,而且商店营业时的日期或营业时间也不相同,因此当我查找营业时间表时,我会从列表中查找与“businessID”和“dayWeek”匹配的结果“营业时间”数据库。

数据库:

  1. “企业”包含 id [Key]、bus_name、bus_address、bus_city、bus_phone。我将结果限制为每页 20 个(显然 20 被变量替换)

数据示例:

  • 1001, ABC, 123 Main Street, New York, 212-555-1212
  • 1002, NBC, 246 Elm Street, Long Island, 516-555-1212
  • 1003, CBS, 369 Ave P, Brooklyn, 718-555-1212
  1. “hoursopen”包含 id [Key]、“businessID”匹配数据库“businesses”id [Key]、timeZone(调整时区)、dayWeek(仅选择今天的小时数)、openTime 和 closeTime 数据示例:
  • 3、1001、1、5、09:00:00、21:00:00
  • 4、1001、1、6、09:00:00、21:00:00
  • 5、1001、1、7、10:00:00、16:00:00

代码:

<cfquery name="businesses" datasource="businesses"> 
         SELECT id, bus_name, bus_address, bus_city, bus_phone
         FROM businesses
         WHERE id < 20
</cfquery>
            
<!-- 1st Query businesses (id) that were found to search in the hoursopen database, 
note that the two results are not in the same order-->
            <cfscript>
                businessResults=ValueList(businesses.id)
            </cfscript>  
            
<cfquery name="hoursopen" datasource="hoursopen"> 
    SELECT *
    FROM hoursopen
    WHERE businessID IN ( 
                        <cfqueryparam 
                            value="#businessResults#" 
                            cfsqltype="CF_SQL_INTEGER"
                            list="yes" 
                            />
                            ) AND dayWeek = dayOfWeek(Now())      
</cfquery>

<!-- In the results I bolded the BusinessID to Illustrate the relationship.-->

<p>------ BUSINESS LOCATION & PHONE ------ </p>
 <cfoutput query="businesses">
            <strong>Business ID: #id#</strong><br/>
            Business Name: #bus_name#<br/>
            Address: #bus_address#<br/>
            City: #bus_city#<br/>
            Phone: #bus_phone#<br/>
            <hr/>    
            </cfoutput>
    
<p>------ BUSINESS HOURS ------ </p>
<cfoutput query = "hoursopen">
     <p>ID: #id# | <strong>Business ID: #businessID# </strong>| Time Zone: #timeZone# | 
        Day of the Week: #dayWeek# | Store Opens: #openTime# | Store Closes: #closeTime#</p>
     <hr/>
</cfoutput>```

【问题讨论】:

    标签: coldfusion coldfusion-2016


    【解决方案1】:

    如果我按照您的描述进行操作,我可能会按照以下方式进行操作。基本上我会运行业务查询,将这些结果放入一个结构数组中。当我这样做时,我会得到每项业务的时间,并将其放入一系列结构中。代码如下所示。请知道我根本没有测试过,但这是我做这种工作的典型方式:

        <cffunction name="getBusinessHours" returntype="array">
            <cfargument name="_id" type="string" required="yes">
            <cfargument name="_dayWeek" type="string" required="yes">
    
            <cfquery name="_qHours">
                select  * from  hoursopen
                where   businessId = <cfqueryparam value="#_id#" cfsqltype="integer"> and
                        dayWeek = <cfqueryparam value="#_dayWeek#" cfsqltype="varchar">
                order by 
                        dayWeek
            </cfquery>
    
            <cfset _hours = arrayNew(1)>
            <cfloop query="_qHours">
                <cfset _hour = structNew()>
                <cfset _hour['id'] = id>
                <cfset _hour['timeZone'] = timeZone>
                <cfset _hour['dayWeek'] = dayWeek>
                <cfset _hour['openTime'] = openTime>
                <cfset _hour['closeTime'] = closeTime>
    
                <cfset arrayAppend(_hours, _hour)>
            </cfloop>
    
            <cfreturn _hours>
        </cffunction>
    
    
        <cfquery name="_qBusinesses">
            select * from businesses   //limit query as needed
        </cfquery>
    
    
        <cfset _businesses = ArrayNew(1)>
    
        <cfloop query="_qBusinesses">
            <cfset _business = structNew()>
            <cfset _business['id'] = id>
            <cfset _business['name'] = bus_name>
            <cfset _business['address'] = bus_address>
            <cfset _business['city'] = bus_city>
            <cfset _business['phone'] = bus_phone>
    
            <cfset _business['hours'] = getBusinessHours(id, dayOfWeek(Now())>
    
            <cfset arrayAppend(_businesses,_business)>
    
        </cfloop>
    
        <cfoutput>
            <cfloop array="_businesses" index="_b">
                <strong>Business ID: #_b.id#</strong><br/>
                Business Name: #_b.name#<br/>
                Address: #_b.address#<br/>
                City: #_b.city#<br/>
                Phone: #_b.phone#<br/>
                <hr/>    
                    
                
                <p>------ BUSINESS HOURS ------ </p>
                <cfloop array="_b.hours" index="_h" >
                    <p>ID: #_h.id# | <strong>Business ID: #_b.id# </strong>| Time Zone: #_h.timeZone# | 
                        Day of the Week: #_h.dayWeek# | Store Opens: #_h.openTime# | Store Closes: #_h.closeTime#</p>
                    <hr/>
                </cfloop>
            </cfloop>    
        </cfoutput>
    

    【讨论】:

      【解决方案2】:

      您确定需要 QoQ 吗?您没有提及您的 dbms 或配置,但如果两个 db 在同一台服务器上,您可能可以直接从另一个中查询一个。在这种情况下,您只需要一个 JOIN。

       SELECT b.id, b.bus_name, b.bus_address, b.bus_city, b.bus_phone
              , h.timeZone, h.dayOfWeek, h.openTime, h.closeTime
       FROM   databaseA.owner.businesses b
                  LEFT JOIN databaseB.owner.hoursopen h ON h.businessID = b.id 
                  AND h.dayWeek = dayOfWeek(Now())
       WHERE b.id < 20
       ORDER BY b.bus_name, h.dayOfWeek
      

      注意:并非所有商店都有营业时间

      如果不可能,QoQ 可能不是这里的最佳选择,因为它们不支持OUTER JOIN's。一个简单的替代方法是将小时数据转储到结构的结构中,使用业务 ID 作为键。然后只需按商家 ID 查找营业时间。

      Example

      <!--- store hours by business id  --->
      <cfset businessHours = {}>
      <cfloop from="1" to="#hoursOpen.recordCount#" index="x">
          <cfset rowData = hoursOpen.getRow( x )>
          <cfset businessHours[ rowData.businessId ] = rowData>
      </cfloop>
      
       <cfoutput query="businesses">
          <p>------ BUSINESS LOCATION & PHONE ------ </p>
          <strong>Business ID: #id#</strong><br/>
          Business Name: #bus_name#<br/>
          Address: #bus_address#<br/>
          City: #bus_city#<br/>
          Phone: #bus_phone#<br/>
      
          <!--- if we found hours for this business --->
          <cfif businessHours.keyExists( businesses.id )>
              <cfset hoursData = businessHours[ businesses.id ]>
              <p>------ BUSINESS HOURS ------ </p>
              <p>ID: #hoursData.id# 
                  | <strong>Business ID: #hoursData.businessID# </strong>
                  | Time Zone: #hoursData.timeZone# 
                  | Day of the Week: #hoursData.dayWeek# 
                  | Store Opens: #hoursData.openTime# 
                  | Store Closes: #hoursData.closeTime#
              </p>
          </cfif>
      
          <hr/>    
       </cfoutput>
      

      【讨论】:

        【解决方案3】:

        谢谢SOS和snackboy。

        当我接到一个电话说我最好的朋友正在他的坟墓揭幕时,我因 Corona 病了,刚回到现实。非常感谢您的帮助。

        现在我已经尝试了这两个想法,我遇到了一个有趣的概念。您不能在 CFOUTPUT 中执行 CFOUTPUT,但字面上存在漏洞。

        因为数据量这么少,所以创建所有这些数组是一样的。

        <cfloop
            query = "hoursopen">
            <cfif businessID IS businesses.id>
           ...
        </cfloop>
        

        喜欢听到一些反馈。

        【讨论】:

        • 向问题添加后续 cmets 时,最好转到原始帖子的底部并“添加评论”,而不是发布新答案。欢迎来到 SO。
        • 希望你感觉好多了。 TBH,我对不必要的循环过敏(-:. 只是好奇,但为什么不直接将小时数据放入结构中并按键查找?它是 only about 5 lines of code 并且应该比循环中的循环更好地扩展。用限制 20 个业务,结构方法最多执行 40 次迭代,而“循环中的循环”迭代 400 次。这意味着如果您将业务数量增加到 50,后者将迭代 2500 次!
        猜你喜欢
        • 2011-11-09
        • 1970-01-01
        • 2021-03-05
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 2020-11-14
        • 2012-01-21
        相关资源
        最近更新 更多