我有以下查询:
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)
在Interim中,下面的查询会显示所有记录,但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
我已经部分解决了这个问题。我现在能够看到田野。我使用了以下查询,我在'name'表中创建了额外的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
我如何在查询中包含此内容?
答案 0 :(得分:0)
我试图尽可能地破译你的查询,看起来非常难看。但是,让我试着总结一下你可能正在寻找的东西(从第一个原始查询开始)。由于您按学校名称进行分组,因此您需要最新的活动提交日期记录信息。从似乎是一些XML格式的查询结构看来,你似乎只想提交状态为1.你的其他“输入/查询”基本上是在表之间加入以从其他表中获取查找描述,例如month(通过MonthID) ),年(YearID),状态(statusID)等。我已将这些简化为更正常的查询识别,其他任何人都习以为常。我更改了别名以更好地与(学校)名称表的原始表“n”相关联,“提交”表的“s”,月份“m”,年份“y”,“JOS”用户“j”(可以一直是你的用户)。
我把它们留给LEFT-JOIN,因为你声明你想要所有的名字,无论学校提交的任何提交材料。
现在,预先查询。在这里,我运行查询专门按学校“No”分组和max“dateUpdated”分组,但仅限于状态为1的地方。每个学校全面删除重复...每个学校一个记录MAX
从这个PreQuery别名,我使用它来加入学校“No”的实际提交表,继续根据最大日期获得与提交相关的其他细节。
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
学校编号已经与各自的公开提交条目相关联。
对于列输出类型为hidden = y,filter = y等,一旦有了数据,输出机制应该应用(显示与否)数据。
希望这是对原始查询上下文的下降评估。