0

我有以下查询:

SELECT
<output name="s.No" filter="y" title="School EMIS" data="text" width="50" />,
<output name="s.Name" title="School Name" filter="y" data="text" width="130" />,
<output name="s.Area" title="District" width="80" />,
<output name="m.month"  title="Month" width="60"  />,
<output name="t.yearid"  title="Year" width="40" />,
<output name="y.status"  title="Database Status" width="75" />,
<output name="j.name"  title="Updated By" width="60" />,
<output name="t.dateupdated"  title="Date Updated" width="60" hidden="y" />,
<output name="s.statusid"  title="School Submission Status" width="60"  hidden="y" />
FROM name s LEFT JOIN submissions t ON s.No = t.No
LEFT JOIN month m ON t.monthid = m.monthid
LEFT JOIN status y ON y.statusid = t.statusid
LEFT JOIN jos_users j ON t.user = j.id
WHERE statusid = <input title="Select Status" name="statusid"   
type="query" required="y"width="250" query="SELECT DISTINCT  CONCAT(status),statusid 
FROM status   
WHERE statusid = 1 ORDER BY status"  />
AND month = <input title="Select Month" name="monthid" type="query" required="y" 
width="250" query="SELECT DISTINCT CONCAT(month),monthid FROM month ORDER BY monthid" 
/>
AND year = <input title="Select Year" name="yearid" type="query" required="y" 
width="250" 
query="SELECT year FROM year ORDER BY Yearid" />
GROUP BY s.No
ORDER BY max(t.dateupdated) DESC

查询工作正常。它按 3 SELECTS 列出所有详细信息。我需要它列出“名称”和“提交”表中的所有数据,列出“提交”表中的选定数据,但显示“名称”表中不存在数据的所有其他记录

有谁知道我怎么能做到这一点?请协助。

下表结构:

 `Name` table:
 no  - int 11 (Primary Key)
 name - Varchar 255
 area - Varchar 255

 `Submissions` table:

 userid - int (11)
 statusid - int (11)  
 no - int (11) (Primary Key)     
 name - varchar (255)    
 area - varchar (255)    
 month - int (11) 
 year - int (11)    
 dateupdated - datetime (Primary Key)

在过渡期间,下面的查询确实显示了所有记录,但是SELECTS(过滤器)不起作用

我有以下查询:

SELECT
<output name="s.No" filter="y" title="School EMIS" data="text" width="50" />,
<output name="s.Name" title="School Name" filter="y" data="text" width="130" />,
<output name="s.Area" title="District" width="80" />,
<output name="m.month"  title="Month" width="60"  />,
<output name="t.yearid"  title="Year" width="40" />,
<output name="y.status"  title="Database Status" width="75" />,
<output name="j.name"  title="Updated By" width="60" />,
<output name="t.dateupdated"  title="Date Updated" width="60" hidden="y" />
FROM name s LEFT JOIN submissions t ON s.No = t.No
LEFT JOIN month m ON t.monthid = m.monthid
LEFT JOIN status y ON y.statusid = t.statusid
LEFT JOIN jos_users j ON t.user = j.id
WHERE s.no NOT IN (
SELECT status
FROM status
WHERE statusid = <input title="Select Status" name="statusid"   
type="query" required="y"width="250" query="SELECT DISTINCT  CONCAT(status),statusid 
FROM status   
WHERE statusid = 1"  />
)
AND s.no NOT IN (
SELECT month
FROM month
WHERE month = <input title="Select Month" name="monthid" type="query" required="y" 
width="250" query="SELECT DISTINCT CONCAT(month),monthid FROM month ORDER BY monthid" 
/>
)
AND s.no NOT IN (
SELECT year
FROM year
WHERE year = <input title="Select Year" name="yearid" type="query" required="y" 
width="250" 
query="SELECT year FROM year ORDER BY Yearid" />
)
GROUP BY s.No
ORDER BY max(t.dateupdated) DESC

我已经部分解决了这个问题。我现在可以看到字段。我使用了以下查询,我在“名称”表中创建了额外的 NULL 列来实现这一点:

SELECT
<output name="submissions.user" title="User" width="80" />,
<output name="submissions.statusid" title="Status" width="80" />,
<output name="submissions.no"title="no" width="60"  />,
<output name="submissions.Name"  title="School Name" width="40" />,
<output name="submissions.area"  title="District" width="75" />,
<output name="submissions.monthid"  title="Month" width="60" />,
<output name="submissions.yearid"  title="Year" width="60" />,
<output name="submissions.dateupdated"  title="Date Updated" width="60" />
FROM submissions 
RIGHT OUTER JOIN name ON submissions.no = name.no
WHERE submissions.statusid = <input title="Select Status" name="submissions.statusid" 
type="query" required="y" width="250" query="SELECT DISTINCT CONCAT(status),statusid
FROM status WHERE statusid = 1 ORDER BY status"  />
AND submissions.monthid = <input title="Select Month" name="submissions.monthid"      
type="query" required="y" width="250" query="SELECT DISTINCT CONCAT(month),monthid
FROM month ORDER BY monthid" />
AND submissions.yearid = <input title="Select Year" name="submissions.yearid" 
type="query" required="y" width="250" query="SELECT year FROM year ORDER BY Yearid" />

UNION 

SELECT
<output name="name.user" title="User" width="80" />,
<output name="name.statusid" title="Status" width="80" />,
<output name="name.no" filter="y" title="no" data="text" filtertype="grid" width="50"
/>,
<output name="name.Name" title="School Name" filter="y" data="text" width="130" />,
<output name="name.area" title="District" width="80" />,
<output name="name.monthid" title="Month" width="80" />,
<output name="name.yearid" title="Year" width="80" />,
<output name="name.dateupdated" title="Date Updated" width="80" />
FROM name
LEFT OUTER JOIN submissions ON name.no = submissions.no

我现在唯一要做的就是 GROUP 和 ORDER BY

GROUP BY name.No
ORDER BY max(name.dateupdated) DESC

我将如何将其包含在我的查询中?

4

1 回答 1

0

我试图尽可能地破译您的查询,而且看起来非常难看。但是,让我尝试总结一下您可能正在寻找的内容(从第一个原始查询开始)。由于您正在按学校名称进行分组,因此您需要最新的活动提交日期记录信息。从似乎是一些 XML 格式的查询结构来看,您似乎只希望提交状态为 1。您的其他“输入/查询”本质上是在表之间连接以从其他表中获取查找描述,例如月份(通过 MonthID )、年份 (YearID)、状态 (statusID) 等。我已将这些简化为其他任何人都会习惯看到的更正常的查询识别。我更改了别名以更好地与(School)Name 表的原始表“n”关联,“s”

正如您所说,无论学校提交的任何文件如何,我都将它们保留为 LEFT-JOIN。

现在,预查询。在这里,我运行了按学校“No”和最大“dateUpdated”专门分组的查询,但仅在状态为 1 的情况下运行。这消除了每所学校的全面重复......每所学校的一个记录 MAX .

从这个 PreQuery 别名中,我使用它来加入学校“否”上的实际提交表,以根据该最大日期继续获取与提交相关的其余详细信息。

SELECT
      n.No SchoolEMIS,
      n.Name SchoolName,
      n.Area District,
      m.month,
      y.Year,
      s.statusID SchoolSubmissionStatus,
      st.status DatabaseStatus,
      j.name UpdatedBy,
      PreQuery.MaxUserDate dateUpdated
   from
      name n
         left join ( select s1.No,
                            MAX( s1.dateupdated ) as MaxUserDate
                         from
                            submissions s1
                         where
                            s1.statusID = 1
                         group by
                            s1.No ) preQuery
            ON n.No = PreQuery.No
            left join submissions s
               ON PreQuery.No = s.No
               AND PreQuery.MaxUserDate = s.dateupdated
               left join month m
                  ON s.monthid on m.monthid
               left join year y
                  ON s.yearid = y.yearid
               left join status st
                  ON s.statusid = st.statusid
               left join jos_users j
                  ON s.userid = j.id
   order by
       PreQuery.MaxUserDate DESC

我看到的一种可能的修饰符。如果给定学校有多个状态为 1 的提交记录,并且您想查看状态为 1 的学校的所有条目,则只需更改

AND PreQuery.MaxUserDate = s.DateUpdated

AND s.status = 1

学校编号已经与他们各自的公开提交条目相关联。

至于隐藏=y、过滤器=y等列输出类型,一旦你有了数据,你的输出机制应该应用(显示或不显示)数据。

希望这是对您的原始查询上下文的下降评估。

于 2013-12-04T19:28:21.173 回答