Question : confused by SQL query

I am working though one of the most (for me) confusing parts of a rebuild/redevelopment for our content management system.

In the query

====begin 1st  code example block =====
<cfquery datasource="DSN_Vineyard" name="ministry_update">
      SELECT article_date as update_date, article_title as update_title, created_by as ministry_leader,
            ministry_desc, article_id, mission_statement, author_name,
            (SELECT rtrim(first_name) + ' ' + rtrim(last_name) from users where user_id = created_by) as u_by,
            file_content1, file_content2, file_content3
      FROM text_modalities, ministries
      WHERE text_modalities.ministrY_id = ministries.ministry_id
      AND ministries.ministry_id = #ministry_id#
      AND text_modalities.article_type = 'M'
      <cfif isDefined("article_id") and val(article_id)>  
   AND article_id = #val(article_id)#      
      <cfelse>      AND article_id = (SELECT max(article_id) FROM text_modalities x WHERE article_type = 'M'
            AND x.ministry_id = ministries.ministry_id AND article_date = (SELECT max(article_date)
            FROM text_modalities y WHERE article_type = 'M' AND Y.ministry_id = ministries.ministry_id))            
      </cfif>
</cfquery>

<cfif ministry_update.recordcount gt 0>
<cfquery datasource="DSN_Vineyard" name="article_sections">
      SELECT * FROM text_modules WHERE article_id = #ministry_update.article_id#
</cfquery>
</cfif>

=====end 1st code example block ====






I know what is going on/being done - until the code gets to the part where it does a conditional statement if the article_id is not defined.

=====begin 2nd code example block ====

<cfif isDefined("article_id") and val(article_id)>  
   AND article_id = #val(article_id)#      
      <cfelse>      AND article_id = (SELECT max(article_id) FROM text_modalities x WHERE article_type = 'M'
            AND x.ministry_id = ministries.ministry_id AND article_date = (SELECT max(article_date)
            FROM text_modalities y WHERE article_type = 'M' AND Y.ministry_id = ministries.ministry_id))            
      </cfif>

====end 2nd code example block====



I don't understand what the "x" and the "y" are and what they are supposed to be doing.

I know that you can create references to tables that don't actually exist, or something like that - but I know that there are no tables/columns that are called 'x' or 'y' - so if they are legit, this is what they are. Furthermore, if I try to execute these apparent sub-queries by themselves - I get errors; so I don't think they are coded correctly. I think that is is possible that these have ever worked correctly.



When a new ministry update is added, it is initially added to a page_entities column, and the id is captured, and this is used as the tracking variable for all pages to the website. Then, the ministry update is added to the text_modalities table. Later, the user can create text sections (which represent content sections) and these are stored in text_modules.

essentially the second query should be looking for the text sections in text_modules associated with this page in text_modalties.


The page is called by its modality archetype ("M") and it's ministry_id. (144)

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
<!--- here for reference purposes

<cfqueryparam cfsqltype="cf_sql_integer" value="#select_no#">
<cfqueryparam cfsqltype="cf_sql_integer" value="#church_id#">
<cfqueryparam cfsqltype="cf_sql_integer" value="#gallery_id#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#ministry_id#">
<cfqueryparam cfsqltype="cf_sql_varchar" value="#new_pass#">
<cfqueryparam cfsqltype="cf_sql_varchar" value="#login_pass#">
<cfqueryparam cfsqltype="cf_sql_varchar" value="#login_name#">

<cfqueryparam cfsqltype="cf_sql_varchar" value="#article_id#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#calendar_id#">
<cfqueryparam cfsqltype="cf_sql_varchar" value="#pagename#">
<cfqueryparam cfsqltype="cf_sql_integer" value="#church_id#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#user#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#session.user_id#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#thread_id#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#user_id#">
<cfqueryparam cfsqltype="cf_sql_varchar" value="#namesearch#">

<cfqueryparam cfsqltype="cf_sql_varchar" value="#eventtype#">
<cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#firstdate#">
<cfqueryparam cfsqltype="cf_sql_bigint" value="#article_id#"> 
<cfqueryparam cfsqltype="cf_sql_integer" value="#ministry_id#">   
<cfqueryparam cfsqltype="cf_sql_integer" value="#gallery_id#">

	
--->


<cfparam name="get_bio" default="yes">
<cfparam name="spanclass" default="text_medium">
<cfparam name="article_id" default="not_present">

<!---
<cfoutput>
DEBUG BEGIN:
my article_id = "#article_id#"<br />
my ministry_id = "#ministry_id#"<br />
END DEBUG<br />
</cfoutput>
--->


<!---
<cfquery datasource="DSN_Vineyard" name="ministry_update">
	SELECT article_date as update_date, article_title as update_title, created_by as ministry_leader, 
		ministry_desc, article_id, mission_statement, author_name,
		(SELECT rtrim(first_name) + ' ' + rtrim(last_name) from users where user_id = created_by) as u_by, 
		file_content1, file_content2, file_content3
	FROM articles2, ministries
	WHERE articles2.ministrY_id = ministries.ministry_id
	AND ministries.ministry_id = #ministry_id#
	AND articles2.article_type = 'M'
	<cfif isDefined("article_id") and val(article_id)>  
   AND article_id = #val(article_id)#	
	<cfelse>	AND article_id = (SELECT max(article_id) FROM articles2 x WHERE article_type = 'M'
		AND x.ministry_id = ministries.ministry_id AND article_date = (SELECT max(article_date) 
		FROM articles2 y WHERE article_type = 'M' AND Y.ministry_id = ministries.ministry_id))		
	</cfif>
</cfquery>

<cfif ministry_update.recordcount gt 0>
<cfquery datasource="DSN_Vineyard" name="article_sections">
	SELECT * FROM article_section WHERE article_id = #ministry_update.article_id#
</cfquery>
</cfif>
--->


<!--- new query --->
<!--- articles2 are now text_modalities & article_section is now text_modules --->


<cfquery datasource="DSN_Vineyard" name="ministry_update">
	SELECT article_date as update_date, article_title as update_title, created_by as ministry_leader, 
		ministry_desc, article_id, mission_statement, author_name,
		(SELECT rtrim(first_name) + ' ' + rtrim(last_name) from users where user_id = created_by) as u_by, 
		file_content1, file_content2, file_content3
	FROM text_modalities, ministries
	WHERE text_modalities.ministrY_id = ministries.ministry_id
	AND ministries.ministry_id = #ministry_id#
	AND text_modalities.article_type = 'M'
	<cfif isDefined("article_id") and val(article_id)>  
   AND article_id = #val(article_id)#	
	<cfelse>	AND article_id = (SELECT max(article_id) FROM text_modalities x WHERE article_type = 'M'
		AND x.ministry_id = ministries.ministry_id AND article_date = (SELECT max(article_date) 
		FROM text_modalities y WHERE article_type = 'M' AND Y.ministry_id = ministries.ministry_id))		
	</cfif>
</cfquery>

<cfif ministry_update.recordcount gt 0>
<cfquery datasource="DSN_Vineyard" name="article_sections">
	SELECT * FROM text_modules WHERE article_id = #ministry_update.article_id#
</cfquery>
</cfif>


<cfoutput> mr #ministry_update.article_id# xx <cfif (get_bio is "yes") and (ministry_update.recordcount gt 0)> 
<!-- QUERY FOR THE AUTHOR'S PICTURE --> <CFQUERY datasource="DSN_Vineyard" name="biopic"> 
SELECT * from biographies where user_id = #ministry_update.ministry_leader# </cfquery> 
<cfloop query="biopic"> <cfif photo_file is not ""> <img alt="" src="images/#photo_file#" width=40 border="2" align="left" hspace="5"> 
</cfif> </cfloop> </cfif> <span class="caption"> #ministry_update.update_title# 
</span><br> <Cfif trim(ministry_update.file_content1) is not ""> <span class="text_small"> 
<a href="#ministry_update.file_content1#" target="_blank"> <cfif (trim(ministry_update.file_content1) contains ".wav") or (trim(ministry_update.file_content1) contains ".avi") or (trim(ministry_update.file_content1) contains ".wmv") or (trim(ministry_update.file_content1) contains ".wma") or (trim(ministry_update.file_content1) contains ".cda") or (trim(ministry_update.file_content1) contains ".asx") or (trim(ministry_update.file_content1) contains ".mp3") or (trim(ministry_update.file_content1) contains ".asx") or (trim(ministry_update.file_content1) contains ".asf") or (trim(ministry_update.file_content1) contains ".mpeg") or (trim(ministry_update.file_content1) contains ".mpg") or (trim(ministry_update.file_content3) contains ".mov") or (trim(ministry_update.file_content3) contains ".swf")>	
<img src="images/listen.gif" border="0"> listen <cfelse> <img src="images/document.gif" border="0"> 
attachment </cfif> </a> </span> &nbsp;&nbsp;&nbsp; </cfif> <Cfif trim(ministry_update.file_content2) is not ""> 
<span class="text_small"> <a href="#ministry_update.file_content2#" target="_blank"> 
<cfif (trim(ministry_update.file_content2) contains ".wav") or (trim(ministry_update.file_content2) contains ".avi") or (trim(ministry_update.file_content2) contains ".wmv") or (trim(ministry_update.file_content2) contains ".wma") or (trim(ministry_update.file_content2) contains ".cda") or (trim(ministry_update.file_content2) contains ".asx") or (trim(ministry_update.file_content2) contains ".mp3") or (trim(ministry_update.file_content2) contains ".asx") or (trim(ministry_update.file_content2) contains ".asf") or (trim(ministry_update.file_content2) contains ".mpeg") or (trim(ministry_update.file_content2) contains ".mpg") or (trim(ministry_update.file_content3) contains ".mov") or (trim(ministry_update.file_content3) contains ".swf")>	
<img src="images/listen.gif" border="0"> - listen <cfelse> <img src="images/document.gif" border="0"> 
attachment </cfif> </a> </span> &nbsp;&nbsp;&nbsp; </cfif> <Cfif trim(ministry_update.file_content3) is not ""> 
<span class="text_small"> <a href="#ministry_update.file_content3#" target="_blank"> 
<cfif (trim(ministry_update.file_content3) contains ".wav") or (trim(ministry_update.file_content3) contains ".avi") or (trim(ministry_update.file_content3) contains ".wmv") or (trim(ministry_update.file_content3) contains ".wma") or (trim(ministry_update.file_content3) contains ".cda") or (trim(ministry_update.file_content3) contains ".asx") or (trim(ministry_update.file_content3) contains ".mp3") or (trim(ministry_update.file_content3) contains ".asx") or (trim(ministry_update.file_content3) contains ".asf") or (trim(ministry_update.file_content3) contains ".mpeg") or (trim(ministry_update.file_content3) contains ".mpg") or (trim(ministry_update.file_content3) contains ".mov") or (trim(ministry_update.file_content3) contains ".swf")>	
<img src="images/listen.gif" border="0"> - listen <cfelse> <img src="images/document.gif" border="0"> 
attachment </cfif> </a> </span> &nbsp;&nbsp;&nbsp; </cfif> <cfif (trim(ministry_update.file_content1) is not "") or (trim(ministry_update.file_content2) is not "") or (trim(ministry_update.file_content3) is not "")> 
<br><br> </cfif> <cfif ministry_update.recordcount gt 0> <cfloop query="article_sections"> 
<cfif trim(picture) is not ""><img src="http://www.#fromsite#.#ext#/images/#picture#" 
				<cfif (picwidth is not "") and (picwidth is not 0)>	width="#picwidth#"	</cfif> 
				<cfif (picheight is not "") and (picheight is not 0)>	height="#picheight#" </cfif>	
				<cfif picborder is "">	border="0"	<cfelse>  border="#picborder#"	</cfif>
				<cfif picspace is "">	vspace="0" hspace="0"	<cfelse>	vspace="#picspace#" hspace="#picspace#"	</cfif>
				<cfif picjustify is not "">		align="#picjustify#"		</cfif> 
		> </cfif> <cfif trim(heading) is not ""> <span class="caption">	
#heading# </span> <br> </cfif> <span class="#spanclass#"> #ParagraphFormat(section_content)#	
</span> <br><Br> </cfloop> </cfif> <cfif ministry_update.recordcount eq 0> <p><span class="text_medium"><i>No 
information is currently available.</i></span><br><br></p></cfif> </cfoutput>
Attachments:
 
db column for ministries
 
 
db column for text_modalities
 
 
db column for text_modules
 
 
db column for users
 

Answer : confused by SQL query

You mean this query by itself fails?

SELECT max(article_date)
            FROM text_modalities y
WHERE article_type = 'M' AND
Y.ministry_id =ministries.ministry_id

that is a given, because as you can see "ministries" does not exist in this subquery.  However, taken with the outer query, "ministries" refers to the outer query - this is called a correlation, and it works.  It is like a join between inner and outer query.

As for "y" in this case, it is an alias given to the "textmodalities" table in this subquery, so that you can access the column names like Y.ministry_id as shown.  You should always use it if using multiple tables in query, and definitely if repeating the table name more than once in the full query
Random Solutions  
 
programming4us programming4us